socket.io 与 express [英] socket.io with express

查看:50
本文介绍了socket.io 与 express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我正在使用 socket.io 和 express ,

i have a project and I'm using socket.io with express ,

所以我需要(我试过)是广播一条消息,但来自一个明确的行动.这可能吗,我不知道如何获得发送或广播的参考.

so what i need (i tried) is broadcasting a message but from an express action. is this possible i don't know how to get a reference to send or broadcast.

app.get('/', function(req, res) {
//i need to send messages from here 
});

其他的东西,比如同时使用 express+socket.io 和我一起工作:)

Other things like using both express+socket.io is working with me :)

推荐答案

你可能想看看我的 socket.io + Express 入门.您想要的内容在那里有详细介绍.

You might want to have a look at my socket.io + Express primer. What you want is covered in detail there.

// Send to all connected sockets
io.sockets.send('Hello, World!');

// Send to all sockets in a specified room
io.sockets.in('room').send('Hello, Room!');

其中 io 是调用 socketio.listen() 返回的值.您可以将该代码放置在您的应用程序中的任何位置,例如在您的 app.get 回调中.

Where io is the value returned by the call to socketio.listen(). You can place that code anywhere in your application, eg in your app.get callbacks.

这篇关于socket.io 与 express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆