杰迪socket.io:从服务器端捕获和发出事件 [英] GeddyJS & socket.io: catching and emitting events from server side
问题描述
我想和Geddy一起使用socket.io.我只是按照以下链接中的说明进行操作:
i would like to use socket.io along with Geddy. I just followed the instructions in the below link:
https://github.com/mde/geddy/wiki/Getting-started-with-Geddy,-Socket.io,-and-Authentication
建议我如何在服务器端捕获特定模型上的连接"事件.
Suggest me how to catch the 'connection' event on specific model in server-side.
还可以从下面一窥我到目前为止使用此模型所做的事情...
Also find below the glimpse of what i have did so far with this model...
geddy scaffold -rt LiveUpdate stat:string category:string
并在
geddy.io.addListenersForModels(['LiveUpdate']);
geddy.model.LiveUpdate.on('update', function (chat) {
....
我真正需要的是知道如何从服务器端捕获或发出此模型的事件.
What i actually need is to know how to catch or emit events for this model from the server-side.
推荐答案
从Geddy中的模型和控制器发出socket.io事件非常简单:
Emitting socket.io events from models and controllers in Geddy is pretty simple:
-
geddy.io.sockets.emit
将向所有连接的客户端发出事件
geddy.io.sockets.emit
will emit an event to all connected clients
如果要侦听来自特定客户端的事件或将事件发送到特定客户端,则需要在应用程序的config目录中创建一个after_start.js
文件,并像通常使用socket.io
一样使用geddy.io
:
If you want to listen for events from, or send events to specific clients, you'll need to create an after_start.js
file in your app's config directory, and use geddy.io
like you would normally use socket.io
:
geddy.io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
这篇关于杰迪socket.io:从服务器端捕获和发出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!