Skip to content

Commit

Permalink
fix: remove logs and add back remove pair logic; fixed ws server (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino authored Nov 6, 2024
1 parent 0471859 commit b6294a2
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { createServer } from 'http';
import { WebSocketServer, type RawData, type WebSocket } from 'ws';
import crypto from 'crypto';
import qs from 'qs';
import { Mutex } from 'async-mutex';
const mutex = new Mutex();

const app = express();
const port = process.env.PORT || 3000;
Expand Down Expand Up @@ -263,15 +261,15 @@ wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
if (!clientId.includes(':proof')) {
const pair = pairs.get(clientId);
if (pair) {
// pairs.delete(pair);
// pairs.delete(clientId);
// await send(
// pair,
// bufferify({
// method: 'pair_disconnect',
// params: { pairId: clientId },
// }),
// );
pairs.delete(pair);
pairs.delete(clientId);
await send(
pair,
bufferify({
method: 'pair_disconnect',
params: { pairId: clientId },
}),
);
}
}

Expand Down Expand Up @@ -324,6 +322,8 @@ wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
}
break;
}
case 'ping':
break;
default:
console.log('unknown msg', msg);
break;
Expand All @@ -339,27 +339,25 @@ wss.on('connection', async (client: WebSocket, request: IncomingMessage) => {
}

async function send(clientId: string, data: RawData) {
return mutex.runExclusive(async () => {
return 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);
});
}
});
}),
(err) => {
resolve(false);
},
);
} else {
target.send(data, (err) => {
resolve(!err);
});
}
});
}
});
Expand Down

0 comments on commit b6294a2

Please sign in to comment.