node.js socket.io 与 express
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4096946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.io with express
提问by ibmkhd
i have a project and I'm using socket.io with express ,
我有一个项目,我正在使用 socket.io 和 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
});
Other things like using both express+socket.io is working with me :)
其他的事情,比如同时使用 express+socket.io 和我一起工作:)
回答by Philippe Rathé
As long as I understand,
只要我明白,
Why not use the socket message type as an event instead of a http get or post? On the client side you would send a message via the websocket with let's say an event property.
为什么不使用套接字消息类型作为事件而不是 http get 或 post?在客户端,您将通过 websocket 发送一条消息,让我们说一个事件属性。
So in your case:
所以在你的情况下:
<script>
// Initialize socket.io ...
// and then
socket.send({event: 'homepage loaded', foo: 'bar'});
</script>
And on the server side:
在服务器端:
var io = io.listen(server);
io.on('connection', function (client) {
client.on('message', function (message) {
if (message.event == 'homepage loaded') {
client.broadcast(...);
}
});
});
回答by Daniel Baulig
You might want to have a look at my socket.io + Express primer. What you want is covered in detail there.
您可能想看看我的socket.io + Express 入门。你想要的在那里有详细的介绍。
// 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!');
Where iois the value returned by the call to socketio.listen(). You can place that code anywhere in your application, eg in your app.getcallbacks.
io调用返回的值在哪里socketio.listen()。您可以将该代码放置在您的应用程序中的任何位置,例如在您的app.get回调中。
回答by Shripad Krishna
Check out my example repo where I use ExpressJS + Juggernaut(pubsub over socket.io):
查看我使用 ExpressJS + Juggernaut(通过 socket.io 发布订阅)的示例存储库:
http://github.com/shripadk/express-juggernaut-demo
http://github.com/shripadk/express-juggernaut-demo
This might be overkill for what you need as it uses Publish/Subscribe. But it does, to a certain extent, solve your issue of using regular ExpressJS routes. Checkout the master branch after cloning the repository:
由于它使用发布/订阅,这对于您所需要的可能有点过头了。但它确实在一定程度上解决了您使用常规 ExpressJS 路由的问题。克隆存储库后签出主分支:
git checkout master
git checkout master
回答by ibmkhd
I Found a nice example how to make what i need but with faye it's here http://nodecasts.org/.
我找到了一个很好的例子,如何制作我需要的东西,但是和 faye 一起,它在这里http://nodecasts.org/。
I don't know the difference between Juggernaut ,Faye and direct Socket.io but Faye is good
我不知道 Juggernaut、Faye 和直接 Socket.io 之间的区别,但 Faye 很好
for my case .And i think both of them use Socket.io internally.
就我而言。我认为他们都在内部使用 Socket.io。

