Skip to content

Commit 0471859

Browse files
authored
chore: disable delete pair for debugging ws server (#37)
1 parent 76a1fa7 commit 0471859

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

server/index.tsx

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ server.listen(port, () => {
237237
const clients: Map<string, WebSocket> = new Map<string, WebSocket>();
238238
const pairs: Map<string, string> = new Map<string, string>();
239239

240-
wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
240+
wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
241241
const query = qs.parse((request.url || '').replace(/\/\?/g, ''));
242242
const clientId = (query?.clientId as string) || crypto.randomUUID();
243243
clients.set(clientId, client);
244244
console.log(`New Connection - ${clientId}`);
245245

246246
if (!clientId.includes(':proof')) {
247-
console.log('proof connection', clientId);
248-
client.send(
247+
await send(
248+
clientId,
249249
bufferify({
250250
method: 'client_connect',
251251
params: { clientId },
@@ -263,16 +263,15 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
263263
if (!clientId.includes(':proof')) {
264264
const pair = pairs.get(clientId);
265265
if (pair) {
266-
pairs.delete(pair);
267-
pairs.delete(clientId);
268-
console.log('disconnect', clientId);
269-
await send(
270-
pair,
271-
bufferify({
272-
method: 'pair_disconnect',
273-
params: { pairId: clientId },
274-
}),
275-
);
266+
// pairs.delete(pair);
267+
// pairs.delete(clientId);
268+
// await send(
269+
// pair,
270+
// bufferify({
271+
// method: 'pair_disconnect',
272+
// params: { pairId: clientId },
273+
// }),
274+
// );
276275
}
277276
}
278277

@@ -286,11 +285,6 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
286285
if (!msg) {
287286
const [cid] = clientId.split(':');
288287
const pairedClientId = pairs.get(cid);
289-
// @ts-ignore
290-
console.log('mpc', rawData.length, {
291-
from: clientId,
292-
to: pairedClientId + ':proof',
293-
});
294288
await send(pairedClientId + ':proof', rawData);
295289
return;
296290
}
@@ -319,11 +313,11 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
319313
case 'proof_request_cancel':
320314
case 'proof_request_reject':
321315
case 'proof_request_end':
322-
console.log(msg.method, { from: clientId, to });
316+
console.log('method:', msg.method);
323317
await send(to, rawData);
324318
break;
325319
case 'pair_request_success': {
326-
console.log(msg.method, { from: clientId, to });
320+
console.log('method:', msg.method);
327321
if (await send(to, rawData)) {
328322
pairs.set(to, clientId);
329323
pairs.set(clientId, to);
@@ -345,25 +339,27 @@ wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
345339
}
346340

347341
async function send(clientId: string, data: RawData) {
348-
return new Promise((resolve) => {
349-
const target = clients.get(clientId);
350-
351-
if (!target) {
352-
client.send(
353-
bufferify({
354-
error: {
355-
message: `client "${clientId}" does not exist`,
342+
return mutex.runExclusive(async () => {
343+
return new Promise((resolve) => {
344+
const target = clients.get(clientId);
345+
346+
if (!target) {
347+
client.send(
348+
bufferify({
349+
error: {
350+
message: `client "${clientId}" does not exist`,
351+
},
352+
}),
353+
(err) => {
354+
resolve(false);
356355
},
357-
}),
358-
(err) => {
359-
resolve(false);
360-
},
361-
);
362-
} else {
363-
target.send(data, (err) => {
364-
resolve(!err);
365-
});
366-
}
356+
);
357+
} else {
358+
target.send(data, (err) => {
359+
resolve(!err);
360+
});
361+
}
362+
});
367363
});
368364
}
369365
});

0 commit comments

Comments
 (0)