Last updated 5 years ago
Was this helpful?
这仅在启用了轮询传输(默认情况下)时才有效。将WebSocket用作传输时,不会附加自定义头。这是因为WebSocket握手不支持自定义头文件。(见)
轮询
WebSocket
客户端:
const socket=io({ transportOptions:{ polling:{ extraHeaders:{ "x-clientId":"abc" } } } })
服务端
const io= require('socket.io')() // 中间器件 io.use((socket,next)=>{ const clientId= socket.handshake.headers['x-clientId']; if (isValid(clientId)){ return next() } return next(new Error("authentication error")) })