问题
I am trying to overwrite generateId. I used for that the example from the docs. But the behaviour is strange. generateId
gets executed, but socket.id
is like a regular id, instead of the desired result "custom:id:" + 1
.
Code
const express = require('express');
const socketIO = require('socket.io');
const http = require('http');
const app = express();
app.set('port', 5000);
const server = http.Server(app);
const io = socketIO(server);
io.engine.generateId = (req) => {
console.log('This here gets printed')
return "custom:id:" + 1;
}
io.on('connection', socket => {
console.log(socket.id);
})
server.listen(5000, async () => {
console.log('Starting server on port 5000');
});
Expected Output: custom:id:1
Actual Output: MNWAC86CdPXB7thTAAAA
package.json Dependencies
"dependencies": {
"base64id": "^2.0.0",
"clean-webpack-plugin": "^3.0.0",
"cookie": "^0.4.1",
"css-loader": "^5.0.1",
"express": "^4.17.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^4.5.0",
"jquery": "^3.5.1",
"ngrok": "^3.4.0",
"sass": "^1.29.0",
"sass-loader": "^10.1.0",
"socket-io": "^1.0.0",
"socket.io": "^3.0.3",
"socket.io-client": "^3.0.3",
"style-loader": "^2.0.0",
"webpack": "^5.7.0",
"webpack-dev-middleware": "^4.0.2"
}
来源:https://stackoverflow.com/questions/65471836/socket-io-overwritten-generateid-gets-executed-but-the-returned-id-seems-to