0

I am trying to send to clients whenever a new connection issued.

My server side javascripts is :

var clients = 0;
io.sockets.on('connection', function (socket) {
clients ++;
console.log(clients);
socket.broadcast.emit('connect',socket.clients);
}

and my client side js to handle:

socket.on('connect', function (data) {
console.log('on'+data);});

the output is undefined (on undefined)

Where 's things wrong?

4

1 回答 1

3
socket.broadcast.emit('connect',socket.clients);
                                ^^^^^^^^^^^^^^ this should be 'clients'

此外,connect是一个带有 的预定义事件socket.io,因此您不应重复使用它,否则它可能会产生意想不到的结果。

FWIW:将向当前连接之外的socket.broadcast.emit所有连接发送消息。如果这不是您想要的(您希望将消息发送到所有连接),请改用)。io.sockets.emit

于 2013-10-20T08:24:47.363 回答