Skip to content

Commit

Permalink
chore: add log for debugging ws server (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino authored Nov 6, 2024
1 parent cd4b22a commit ce18598
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,6 @@ const clients: Map<string, WebSocket> = new Map<string, WebSocket>();
const pairs: Map<string, string> = new Map<string, string>();

wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
// you have a new client
console.log('New Connection');
// add this client to the clients array

const query = qs.parse((request.url || '').replace(/\/\?/g, ''));
const clientId = (query?.clientId as string) || crypto.randomUUID();
clients.set(clientId, client);
Expand Down Expand Up @@ -288,6 +284,8 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
if (!msg) {
const [cid] = clientId.split(':');
const pairedClientId = pairs.get(cid);
// @ts-ignore
console.log('mpc', rawData.length);
await send(pairedClientId + ':proof', rawData);
return;
}
Expand Down Expand Up @@ -316,9 +314,11 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
case 'proof_request_cancel':
case 'proof_request_reject':
case 'proof_request_end':
console.log(msg.method);
await send(to, rawData);
break;
case 'pair_request_success': {
console.log(msg.method);
if (await send(to, rawData)) {
pairs.set(to, clientId);
pairs.set(clientId, to);
Expand All @@ -340,28 +340,25 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
}

async function send(clientId: string, data: RawData) {
return mutex.runExclusive(async () => {
const res = await new Promise((resolve) => {
const target = clients.get(clientId);

if (!target) {
client.send(
bufferify({
error: {
message: `client "${clientId}" does not exist`,
},
}),
(err) => {
resolve(false);
return new Promise((resolve) => {
const target = clients.get(clientId);

if (!target) {
client.send(
bufferify({
error: {
message: `client "${clientId}" does not exist`,
},
);
} else {
target.send(data, (err) => {
resolve(!err);
});
}
});
return res;
}),
(err) => {
resolve(false);
},
);
} else {
target.send(data, (err) => {
resolve(!err);
});
}
});
}
});
Expand Down

0 comments on commit ce18598

Please sign in to comment.