简体   繁体   中英

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...

So I want to send messages to rooms that are not models (about 40 rooms).. How to do that with 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

When you want to send message to all sockets connected to myCustomRoomName use this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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