Compare commits

...

4 commits

3 changed files with 31 additions and 13 deletions

View file

@ -9,7 +9,13 @@
UnplugIcon,
UserIcon,
} from '@lucide/svelte';
import { ConnectionState, type Message, MessageType, type ReceiveHl7v2Message } from '@hnu.de/hl7v2-shared';
import {
ConnectionState,
type DeliverySuccessMessage,
type Message,
MessageType,
type ReceiveHl7v2Message,
} from '@hnu.de/hl7v2-shared';
import { Button } from '$lib/components/ui/button';
import { Card, CardContent, CardHeader } from '$lib/components/ui/card';
import { Input } from '$lib/components/ui/input';
@ -25,7 +31,7 @@
// client state
let composedMessage = $state(''); // content of text-box
let sentMessages = $state<ReceiveHl7v2Message[]>([]); // sent messages stored as type ReceiveHl7v2Message because of the timestamp
let sentMessages = $state<DeliverySuccessMessage[]>([]);
let receivedMessages = $state<ReceiveHl7v2Message[]>([]);
let isSending = $state(false);
let copySuccess = $state(false);
@ -96,6 +102,12 @@
receivedMessages = [message, ...receivedMessages];
break;
// our message was successfully delivered
case MessageType.delivery_success:
sentMessages = [message, ...sentMessages];
isSending = false
break;
// message from server due to delivery error
case MessageType.delivery_error:
deliveryError = message.payload.error;
@ -154,15 +166,6 @@
payload: { message },
} as Message;
ws.send(JSON.stringify(messageToSend));
sentMessages = [{
type: MessageType.receive_hl7v2,
payload: { message, timestamp: new Date().toISOString() },
} as ReceiveHl7v2Message, ...sentMessages];
composedMessage = segmentTemplates.MSH.template();
// reset UI state
isSending = false;
}
// copies the stationId to the clipboard and handles UI state

View file

@ -39,7 +39,7 @@ const clients = new Map();
console.log(`Starting WebSocket server ...`);
console.log("Server configuration:", config);
console.log('Server configuration:', config);
wss.on('connection', (ws) => {
@ -105,17 +105,29 @@ wss.on('connection', (ws) => {
// check if recipient exists and is connected
if (recipientWs && recipientWs.readyState === WebSocket.OPEN) {
const timestamp = new Date().toISOString();
// The recipient is connected. Forward the message.
const forwardMessage = {
type: MessageType.receive_hl7v2,
payload: {
message: message,
timestamp: new Date().toISOString(),
timestamp: timestamp,
},
} as Message;
recipientWs.send(JSON.stringify(forwardMessage));
console.log(`Forwarded message to ${recipientId}`);
const ackMessage = {
type: MessageType.delivery_success,
payload: {
message: message,
timestamp: timestamp,
},
};
ws.send(JSON.stringify(ackMessage));
console.log(`Sent acknowledgment to ${stationId}`);
} else {
// The recipient is not connected or not found.

View file

@ -11,6 +11,7 @@ export enum MessageType {
assign_id = 'assign_id',
send_hl7v2 = 'send_hl7v2',
receive_hl7v2 = 'receive_hl7v2',
delivery_success = 'delivery_success',
delivery_error = 'delivery_error',
}
@ -19,7 +20,9 @@ export type Message =
| { 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_success, payload: { message: string, timestamp: string }}
| { type: MessageType.delivery_error, payload: { error: string }}
// required to define list of this message type
export type ReceiveHl7v2Message = Extract<Message, { type: MessageType.receive_hl7v2 }>;
export type DeliverySuccessMessage = Extract<Message, { type: MessageType.delivery_success }>;