简体   繁体   English

如何在sails.js中为WebSocket定义自定义房间?

[英]How to define custom rooms for WebSocket in sails.js?

Now in default sails.js 0.12.3 I can use Model.PublicCreate only for predifened models... 现在在默认的sails.js 0.12.3中,我只能将Model.PublicCreate用于预先定义的模型...

So I want to send messages to rooms that are not models (about 40 rooms).. How to do that with sails.js? 因此,我想向非模型房间(约40个房间)发送消息。如何使用sails.js做到这一点?

You need to join socket to your custom room first. 您需要先将套接字连接到自定义房间。 So for example make socket route where user will join to custom room and add this line of code somewhere in your controller: 因此,例如进行套接字路由,用户将在其中加入自定义房间,并将此代码行添加到控制器中的某处:

sails.sockets.join(req, 'myCustomRoomName');

You can add socket to any number of rooms. 您可以将套接字添加到任意数量的房间。 Check this docs for more info about that: http://sailsjs.org/documentation/concepts/realtime/on-the-server 检查此文档以获取有关此信息的更多信息: http : //sailsjs.org/documentation/concepts/realtime/on-the-server

When you want to send message to all sockets connected to myCustomRoomName use this: 当您要将消息发送到连接到myCustomRoomName的所有套接字时,请使用以下命令:

sails.sockets.broadcast("myCustomRoomName", "nameOfEvent", {someData: "can also be just string instead of obj, i prefer objects..."});

More info about this topic: http://sailsjs.org/documentation/reference/web-sockets/sails-sockets 有关此主题的更多信息: http : //sailsjs.org/documentation/reference/web-sockets/sails-sockets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM