我正在尝试为 socket.io 服务器编写测试。
io.on('connection', function (socket) {
socket.on('say', function (msg) {
socket.broadcast.emit('update', msg);
});
});
每个客户端都会收到其他客户端发送的所有消息。我只是想测试一下。
为此,我的测试需要模拟多个客户端。但是 socket.io-client
重用了连接,这让这变得很困难。
我应该如何测试这段代码?
最佳答案
找到了。创建客户端时,使用选项 multiplex: false
:
var client = io("http://localhost:3000", {multiplex: false});
根据文档:
When called,
io()
creates a newManager
for the given URL, and attempts to reuse an existingManager
for subsequent calls, unless themultiplex
option is passed withfalse
. Passing this option is the equivalent of passing'force new connection': true
.
关于testing - 如何测试具有多个客户端的 socket.io 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484764/