HL7-1: rename userId to stationId

This commit is contained in:
Markus Thielker 2025-07-30 12:49:02 +02:00
parent 3e00cf88e7
commit bb3a6a5d8e
3 changed files with 32 additions and 34 deletions

View file

@ -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);
});
});