Skip to content

Commit b6294a2

Browse files
authored
fix: remove logs and add back remove pair logic; fixed ws server (#38)
1 parent 0471859 commit b6294a2

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

server/index.tsx

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { createServer } from 'http';
2020
import { WebSocketServer, type RawData, type WebSocket } from 'ws';
2121
import crypto from 'crypto';
2222
import qs from 'qs';
23-
import { Mutex } from 'async-mutex';
24-
const mutex = new Mutex();
2523

2624
const app = express();
2725
const port = process.env.PORT || 3000;
@@ -263,15 +261,15 @@ wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
263261
if (!clientId.includes(':proof')) {
264262
const pair = pairs.get(clientId);
265263
if (pair) {
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-
// );
264+
pairs.delete(pair);
265+
pairs.delete(clientId);
266+
await send(
267+
pair,
268+
bufferify({
269+
method: 'pair_disconnect',
270+
params: { pairId: clientId },
271+
}),
272+
);
275273
}
276274
}
277275

@@ -324,6 +322,8 @@ wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
324322
}
325323
break;
326324
}
325+
case 'ping':
326+
break;
327327
default:
328328
console.log('unknown msg', msg);
329329
break;
@@ -339,27 +339,25 @@ wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
339339
}
340340

341341
async function send(clientId: string, data: RawData) {
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);
342+
return new Promise((resolve) => {
343+
const target = clients.get(clientId);
344+
345+
if (!target) {
346+
client.send(
347+
bufferify({
348+
error: {
349+
message: `client "${clientId}" does not exist`,
355350
},
356-
);
357-
} else {
358-
target.send(data, (err) => {
359-
resolve(!err);
360-
});
361-
}
362-
});
351+
}),
352+
(err) => {
353+
resolve(false);
354+
},
355+
);
356+
} else {
357+
target.send(data, (err) => {
358+
resolve(!err);
359+
});
360+
}
363361
});
364362
}
365363
});

0 commit comments

Comments
 (0)