GeddyJS & socket.io: catching and emitting events from server side

我的未来我决定 提交于 2020-01-05 07:44:11

问题


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

And found the following auto-generated scripts related to socket.io in "show.html.ejs" of

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.


回答1:


Emitting socket.io events from models and controllers in Geddy is pretty simple:

  • geddy.io.sockets.emit will emit an event to all connected clients

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);
  });
});


来源:https://stackoverflow.com/questions/13644551/geddyjs-socket-io-catching-and-emitting-events-from-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!