HL7-1: add documentation #4
3 changed files with 32 additions and 34 deletions
|
@ -19,7 +19,7 @@
|
|||
|
||||
// connection state
|
||||
let ws = $state<WebSocket | undefined>(undefined);
|
||||
let userId = $state<string | undefined>(undefined);
|
||||
let stationId = $state<string | undefined>(undefined); // stationId assigned by server
|
||||
let connectionState = $state<ConnectionState>(ConnectionState.disconnected);
|
||||
|
||||
// client state
|
||||
|
@ -80,7 +80,7 @@
|
|||
|
||||
// initial message from server assigning ID
|
||||
case MessageType.assign_id:
|
||||
userId = message.payload.userId;
|
||||
stationId = message.payload.stationId;
|
||||
composedMessage = segmentTemplates.MSH.template();
|
||||
connectionState = ConnectionState.connected;
|
||||
break;
|
||||
|
@ -128,7 +128,7 @@
|
|||
|
||||
function handleSendMessage(message: string) {
|
||||
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN || isSending || !userId) {
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN || isSending || !stationId) {
|
||||
console.log('Socket not ready');
|
||||
return;
|
||||
}
|
||||
|
@ -150,9 +150,9 @@
|
|||
isSending = false;
|
||||
}
|
||||
|
||||
function copyUserId() {
|
||||
if (userId) {
|
||||
navigator.clipboard.writeText(userId).then(() => {
|
||||
function copyStationId() {
|
||||
if (stationId) {
|
||||
navigator.clipboard.writeText(stationId).then(() => {
|
||||
copySuccess = true;
|
||||
setTimeout(() => copySuccess = false, 2000);
|
||||
});
|
||||
|
@ -174,7 +174,7 @@
|
|||
<div class="max-w-7xl mx-auto">
|
||||
<main class="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
||||
|
||||
<!-- Left Column: User Info & Logs -->
|
||||
<!-- Left Column: Station Info & Logs -->
|
||||
<div class="space-y-6">
|
||||
<Card>
|
||||
<CardHeader class="flex flex-row items-center">
|
||||
|
@ -182,11 +182,11 @@
|
|||
Your Info
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{#if connectionState === ConnectionState.connected && userId}
|
||||
{#if connectionState === ConnectionState.connected && stationId}
|
||||
<Label class="">Station ID</Label>
|
||||
<div class="flex items-center space-x-2">
|
||||
<Input disabled bind:value={userId}/>
|
||||
<Button variant="outline" size="icon" onclick={copyUserId}>
|
||||
<Input disabled bind:value={stationId}/>
|
||||
<Button variant="outline" size="icon" onclick={copyStationId}>
|
||||
{#if copySuccess}
|
||||
<CheckIcon class={'text-green-400'}/>
|
||||
{:else}
|
||||
|
@ -232,7 +232,7 @@
|
|||
<span>HL7v2 Message Editor</span>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{#if connectionState === ConnectionState.connected && userId}
|
||||
{#if connectionState === ConnectionState.connected && stationId}
|
||||
<div>
|
||||
<div class="flex flex-wrap gap-2 mb-4">
|
||||
{#each segmentTypes as type (type)}
|
||||
|
|
|
@ -49,35 +49,33 @@ wss.on('connection', (ws) => {
|
|||
return;
|
||||
}
|
||||
|
||||
// get ID from pool and assign to user
|
||||
const userId = availableIds.pop();
|
||||
if (!userId) {
|
||||
// get ID from pool and assign to station
|
||||
const stationId = availableIds.pop();
|
||||
if (!stationId) {
|
||||
console.log('Connection rejected: Failed to retrieve ID.');
|
||||
ws.close(1013, 'Server is full. Please try again later.'); // 1013: Try again later
|
||||
return;
|
||||
}
|
||||
|
||||
// store client in map
|
||||
clients.set(userId, ws);
|
||||
clients.set(stationId, ws);
|
||||
|
||||
setTimeout(() => {
|
||||
// send user ID to client
|
||||
const welcomeMessage = {
|
||||
type: 'assign_id',
|
||||
payload: {
|
||||
userId: userId,
|
||||
},
|
||||
} as Message;
|
||||
ws.send(JSON.stringify(welcomeMessage));
|
||||
// send station ID to client
|
||||
const welcomeMessage = {
|
||||
type: 'assign_id',
|
||||
payload: {
|
||||
stationId: stationId,
|
||||
},
|
||||
} as Message;
|
||||
ws.send(JSON.stringify(welcomeMessage));
|
||||
|
||||
console.log(`Client connected. Assigning ID: ${userId}`);
|
||||
}, 0);
|
||||
console.log(`Client connected. Assigning ID: ${stationId}`);
|
||||
|
||||
ws.on('message', (message) => {
|
||||
try {
|
||||
|
||||
const parsedMessage: Message = JSON.parse(message.toString());
|
||||
console.log(`Received message from ${userId}:`, parsedMessage);
|
||||
console.log(`Received message from ${stationId}:`, parsedMessage);
|
||||
|
||||
// We only expect one type of message from clients: 'send_hl7v2'
|
||||
if (parsedMessage.type === MessageType.send_hl7v2) {
|
||||
|
@ -127,22 +125,22 @@ wss.on('connection', (ws) => {
|
|||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to process message from ${userId}:`, error);
|
||||
console.error(`Failed to process message from ${stationId}:`, error);
|
||||
}
|
||||
});
|
||||
|
||||
// listen to disconnects to free IDs
|
||||
ws.on('close', () => {
|
||||
console.log(`Client ${userId} disconnected.`);
|
||||
clients.delete(userId);
|
||||
if (userId) {
|
||||
availableIds.push(userId);
|
||||
console.log(`Client ${stationId} disconnected.`);
|
||||
clients.delete(stationId);
|
||||
if (stationId) {
|
||||
availableIds.push(stationId);
|
||||
}
|
||||
availableIds = shake(availableIds);
|
||||
});
|
||||
|
||||
ws.on('error', (error) => {
|
||||
console.error(`WebSocket error for client ${userId}:`, error);
|
||||
console.error(`WebSocket error for client ${stationId}:`, error);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ export enum MessageType {
|
|||
}
|
||||
|
||||
export type Message =
|
||||
| { type: MessageType.assign_id, payload: { userId: string }}
|
||||
| { type: MessageType.assign_id, payload: { stationId: string }}
|
||||
| { type: MessageType.send_hl7v2, payload: { message: string }}
|
||||
| { type: MessageType.receive_hl7v2, payload: { message: string, timestamp: string }}
|
||||
| { type: MessageType.delivery_error, payload: { error: string }}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue