HL7-1: add comments to code
This commit is contained in:
parent
ad48df97e5
commit
3898715915
4 changed files with 37 additions and 8 deletions
|
@ -17,6 +17,7 @@ for (const prefix of config.prefixes) {
|
|||
}
|
||||
}
|
||||
|
||||
// mixes the array randomly to avoid getting the same ID reassigned
|
||||
function shake(arr: any[]) {
|
||||
for (let i = arr.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
|
@ -71,9 +72,11 @@ wss.on('connection', (ws) => {
|
|||
|
||||
console.log(`Client connected. Assigning ID: ${stationId}`);
|
||||
|
||||
// listen for messages and defined message handling
|
||||
ws.on('message', (message) => {
|
||||
try {
|
||||
|
||||
// parse every message to type Message
|
||||
const parsedMessage: Message = JSON.parse(message.toString());
|
||||
console.log(`Received message from ${stationId}:`, parsedMessage);
|
||||
|
||||
|
@ -81,6 +84,7 @@ wss.on('connection', (ws) => {
|
|||
if (parsedMessage.type === MessageType.send_hl7v2) {
|
||||
|
||||
const { message } = parsedMessage.payload;
|
||||
|
||||
// TODO: validate message
|
||||
|
||||
// get sender and recipient ID
|
||||
|
@ -98,6 +102,7 @@ wss.on('connection', (ws) => {
|
|||
// Find the recipient's WebSocket connection in our map.
|
||||
const recipientWs = clients.get(recipientId);
|
||||
|
||||
// check if recipient exists and is connected
|
||||
if (recipientWs && recipientWs.readyState === WebSocket.OPEN) {
|
||||
|
||||
// The recipient is connected. Forward the message.
|
||||
|
@ -136,6 +141,7 @@ wss.on('connection', (ws) => {
|
|||
if (stationId) {
|
||||
availableIds.push(stationId);
|
||||
}
|
||||
// shake IDs to avoid getting the same ID reassigned
|
||||
availableIds = shake(availableIds);
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue