Implement socket.io v4
This commit is contained in:
parent
1cdacb9d80
commit
1105b072e2
5 changed files with 3180 additions and 1675 deletions
|
@ -1 +1,2 @@
|
|||
PORT=3002
|
||||
CORS_ORIGIN='meet.jit.si'
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
FROM node:16.17-slim
|
||||
FROM node:22-bookworm-slim
|
||||
|
||||
WORKDIR /excalidraw-backend
|
||||
|
||||
|
|
4756
package-lock.json
generated
4756
package-lock.json
generated
File diff suppressed because it is too large
Load diff
39
package.json
39
package.json
|
@ -9,22 +9,22 @@
|
|||
},
|
||||
"private": true,
|
||||
"engines": {
|
||||
"node": ">=14.0.0",
|
||||
"npm": ">=7.0.0"
|
||||
"node": ">=20.0.0",
|
||||
"npm": ">=10.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/debug": "4.1.5",
|
||||
"@types/express": "4.17.11",
|
||||
"@types/node": "14.14.31",
|
||||
"@types/socket.io": "2.1.4",
|
||||
"@types/debug": "4.1.10",
|
||||
"@types/express": "4.17.20",
|
||||
"@types/ms": "0.7.33",
|
||||
"@types/node": "20.8.7",
|
||||
"cross-env": "^7.0.3",
|
||||
"debug": "4.3.1",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "4.17.1",
|
||||
"socket.io": "^2.5.0",
|
||||
"debug": "4.3.4",
|
||||
"dotenv": "^16.0.0",
|
||||
"express": "^4.19.2",
|
||||
"socket.io": "^4.7.2",
|
||||
"socket.io-prometheus-metrics": "^1.0.6",
|
||||
"ts-node-dev": "^1.1.8",
|
||||
"typescript": "4.2.3"
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"typescript": "5.2.2"
|
||||
},
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
|
@ -37,16 +37,15 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@jitsi/eslint-config": "^4.1.0",
|
||||
"@types/dotenv": "^8.2.0",
|
||||
"@typescript-eslint/eslint-plugin": "5.30.5",
|
||||
"@typescript-eslint/parser": "5.30.4",
|
||||
"eslint": "8.1.0",
|
||||
"eslint-plugin-import": "2.25.2",
|
||||
"eslint-plugin-jsdoc": "37.0.3",
|
||||
"eslint-plugin-typescript-sort-keys": "^2.1.0"
|
||||
"@typescript-eslint/eslint-plugin": "6.8.0",
|
||||
"@typescript-eslint/parser": "6.8.0",
|
||||
"eslint": "^8.1.0",
|
||||
"eslint-plugin-import": "2.28.1",
|
||||
"eslint-plugin-jsdoc": "46.8.2",
|
||||
"eslint-plugin-typescript-sort-keys": "^3.1.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"bufferutil": "^4.0.6",
|
||||
"utf-8-validate": "^5.0.9"
|
||||
"utf-8-validate": "^6.0.3"
|
||||
}
|
||||
}
|
||||
|
|
39
src/index.ts
39
src/index.ts
|
@ -2,8 +2,8 @@ import debug from 'debug';
|
|||
import dotenv from 'dotenv';
|
||||
import express from 'express';
|
||||
import http from 'http';
|
||||
import socketIO, { Socket } from 'socket.io';
|
||||
import * as prometheus from 'socket.io-prometheus-metrics';
|
||||
import { Socket, Server } from 'socket.io';
|
||||
//import * as prometheus from 'socket.io-prometheus-metrics';
|
||||
|
||||
const serverDebug = debug('server');
|
||||
|
||||
|
@ -28,25 +28,22 @@ server.listen(port, () => {
|
|||
serverDebug(`listening on port: ${port}`);
|
||||
});
|
||||
|
||||
const io = socketIO(server, {
|
||||
handlePreflightRequest: (req, res) => {
|
||||
const headers = {
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
'Access-Control-Allow-Origin': req.header?.origin ?? 'https://meet.jit.si',
|
||||
'Access-Control-Allow-Credentials': true
|
||||
const corsOptions = {
|
||||
origin: process.env.CORS_ORIGIN,
|
||||
methods: [ 'GET', 'POST' ]
|
||||
};
|
||||
|
||||
res.writeHead(200, headers);
|
||||
res.end();
|
||||
},
|
||||
const io = new Server(server, {
|
||||
allowEIO3: true,
|
||||
cors: corsOptions,
|
||||
maxHttpBufferSize: 20e6,
|
||||
pingTimeout: 60000
|
||||
});
|
||||
|
||||
// listens on host:9090/metrics
|
||||
prometheus.metrics(io, {
|
||||
collectDefaultMetrics: true
|
||||
});
|
||||
// prometheus.metrics(io, {
|
||||
// collectDefaultMetrics: true
|
||||
// });
|
||||
|
||||
io.on('connection', socket => {
|
||||
serverDebug(`connection established! ${socket.conn.request.url}`);
|
||||
|
@ -60,27 +57,27 @@ io.on('connection', socket => {
|
|||
users.splice(users.indexOf(socket), 1);
|
||||
});
|
||||
|
||||
const clients = Object.keys(io.sockets.adapter.rooms[roomID].sockets);
|
||||
const clients = Object.keys(io.sockets.adapter.rooms.get(roomID)?.keys() ?? new Set<string>());
|
||||
|
||||
if (clients.length > userLimit) {
|
||||
clients.forEach((clientKey: string) => {
|
||||
const clientSocket = io.sockets.connected[clientKey];
|
||||
const clientSocket = io.sockets.sockets.get(clientKey);
|
||||
|
||||
if (clientSocket !== undefined) {
|
||||
serverDebug(`${clientSocket} has left the ${roomID} room because the user limit was reached.`);
|
||||
clientSocket.leave(roomID);
|
||||
}
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (io.sockets.adapter.rooms[roomID].length <= 1) {
|
||||
if (io.sockets.adapter.rooms.get(roomID)?.size ?? 0 <= 1) {
|
||||
io.to(`${socket.id}`).emit('first-in-room');
|
||||
} else {
|
||||
socket.broadcast.to(roomID).emit('new-user', socket.id);
|
||||
}
|
||||
io.in(roomID).emit(
|
||||
'room-user-change',
|
||||
Object.keys(io.sockets.adapter.rooms[roomID].sockets)
|
||||
'room-user-change', Array.from(io.sockets.adapter.rooms.get(roomID) ?? [])
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -104,7 +101,7 @@ io.on('connection', socket => {
|
|||
const rooms = io.sockets.adapter.rooms;
|
||||
|
||||
for (const roomID of Object.keys(socket.rooms)) {
|
||||
const clients = Object.keys(rooms[roomID].sockets).filter(id => id !== socket.id);
|
||||
const clients = Array.from(rooms.get(roomID) ?? []).filter(id => id !== socket.id);
|
||||
|
||||
if (roomID !== socket.id) {
|
||||
socket.to(roomID).emit('user has left', socket.id);
|
||||
|
|
Loading…
Reference in a new issue