服务器上的
query
对象根本缺少来自客户端的 handshake
对象。
在客户端的
console.log(socket)
上,query
对象显示在 _opts
中,而 opts
看起来像 -
hostname: "localhost"
path: "/socket.io"
port: "3001"
secure: false
客户
import io from "socket.io-client";
this.liveChatSocket = io.connect('/app_chat', {
transports: 'polling',
query: {
patientId, // string
type: "patient"
}
});
setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
['/api', '/socket.io'],
createProxyMiddleware({
target: 'http://127.0.0.1:3000',
changeOrigin: true,
// agent: {keepAlive: true},
ws: false,
logLevel: 'debug'
})
);
};
反应日志看起来像这样。缺少查询参数
[HPM] POST /socket.io/?EIO=4&transport=polling&t=OrQI-0x&sid=_6nbcQQnHKq5NeQoAAHn -> http://127.0.0.1:3000
服务器
index.js
const makeChatNameSpace = (live_chat) => {
live_chat.on('any', ()=>console.log("LOG socket.io live_chat any"))
live_chat.on('connection', function (chatSocket) {
const { type, patientId, username } = chatSocket.handshake;
console.log('chat.index.socket_live_chat.on.connection. Type - ', type, 'client - ', type === 'patient' ? patientId : username, 'connected');
}
module.exports.makeChatNameSpace = makeChatNameSpace
www 文件
let server = http.createServer(app);
const { Server } = require("socket.io");
const { createAdapter } = require("@socket.io/redis-adapter");
const { createClient } = require("redis");
const { makeChatNameSpace } = require("../chat/index");
const { makePersonaNameSpace } = require("../persona/index");
const pubClient = createClient({ url: "redis://localhost:6379" });
const subClient = pubClient.duplicate();
pubClient.connect()
subClient.connect()
const io = new Server(server, {
adapter: createAdapter(pubClient, subClient),
cors: { origin: "*" }
})
const live_chat = makeChatNameSpace(io.of('/app_chat'))
const persona_builder = makePersonaNameSpace(io.of('/doctor/profile')) // working, returns void and doesn't have any query parameters
...
module.exports = { io, live_chat, persona_builder }
快递日志 握手应该有疑问
socket.handshake = {
headers: {
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
cookie: '_ga_H66X2LFN0B=GS1.1.1706617189.135.1.1706617196.0.0.0; _ga_37ETJEX8DX=GS1.1.1706617189.127.1.1706617
196.0.0.0; _ga=GA1.1.1585282760.1686473298; sid=s%3ANAkstqaOgBRIAYmaX3P7s38jTTGs-hci.lkF7maU78Z8jxUpMckYGxbbtgMsOF
L1EV%2BfaBJcjfw8; languageSelected=en; io=Knk2BcJ2k6pk7TesAAA2; TCSESSIONID=21B11575713C1D775F8B60FE16151256; _gid
=GA1.1.631497038.1706298581',
referer: 'http://localhost:3001/',
connection: 'close',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.5',
accept: '*/*',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0',
host: '127.0.0.1:3000'
},
time: 'Tue Jan 30 2024 17:54:54 GMT+0530 (India Standard Time)',
address: '::ffff:127.0.0.1',
xdomain: false,
secure: false,
issued: 1706617494246,
url: '/socket.io/?EIO=4&transport=polling&t=OrQL07M',
query: [Object: null prototype] {
EIO: '4',
transport: 'polling',
t: 'OrQL07M'
},
auth: {}
chat.index.socket_live_chat.on.connection. Type - undefined client - undefined connected
第二个问题 - 每当有新客户端连接到服务器时,127.0.0.1 上都会有 20 秒的延迟。在 nginx 服务器上工作正常。
问题在于使用多个命名空间。
https://stackoverflow.com/a/74050501/6944264解释了详细的问题以及解决方案。