gpt4 book ai didi

node.js - Socket.io 0.7无法调用broadcast.send?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:12 25 4
gpt4 key购买 nike

在socket.io 0.6中,我有以下示例代码:

var io = require('/usr/lib/node_modules/socket.io');
var server = http.createServer(function(req,res){
var path = url.parse(req.url).pathname;
switch( path ){
....
//core codes
socket.broadcast(data);
}
}).listen(8080);

// ... ...

var socket = io.listen(server);

一切顺利。

但是现在我已经将socket.io更新为0.7,并将socket.broadcast更改为socket.sockets.broadcast.send(data);

遇到异常:无法调用“未定义”的发送?

谁能告诉我如何在不监听某个事件的情况下向每个人发送数据?

也许这是一种方法:

clients = new Array();
socket.on('connection',function(socket){ clients.push(socket);}
// ...
for(s in clients) s.send(data);
// ...

真的需要这样做吗?

谢谢!

最佳答案

在socket.io 0.7中你会:

io.sockets.send() // broadcasts to all connected users
io.sockets.json.send() // broadcasts JSON to all connected users
io.sockets.emit('eventname') // broadcasts and emit

或命名空间

io.of('/namespace').send('msg'); // broadcast to all connect users in /namespace

关于node.js - Socket.io 0.7无法调用broadcast.send?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6993725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com