我设置了正常的socket.io:
客户端:
import io from 'socket.io-client'
const socket = io()
socket.on('connect', () => {
console.log('connected')
socket.emit('message', 'I have connected')
})
socket.on('message', (msg) => console.log(msg))
服务器:
import socket from 'socket.io'
const io = socket(/* httpsServer */)
io.on('connection', (socket) => {
console.log('new connection')
socket.on('message', (msg) => console.log(msg))
socket.emit('message', 'This works!')
io.sockets.emit('message', `This doesn't!`)
})
出于某种原因,通过http而不是https完全发出所有作品。通过https,io.sockets.emit
似乎无效。我错过了什么吗?
答案 0 :(得分:0)
检查相同的套接字服务器模块未安装到 httpServer
和httpsServer
。
我将套接字服务器包装在一个模块中,首先将其连接到https服务器,然后再连接到http服务器。这就是为什么单个套接字会连接但是然后发送给所有人只是试图发送到http套接字的原因。