使用 socket.io 和 Sails.js 发送会话特定的消息 [英] Sending session specific messages with socket.io and sails.js

查看:46
本文介绍了使用 socket.io 和 Sails.js 发送会话特定的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sails.js 框架实现私人聊天功能,但在尝试向特定用户发送消息时遇到了一些问题.

I'm trying to implement a private chat functionality, using sails.js framework, and i'm having some troubles when trying so send a message to a particular user.

目前,我已经通过使用 socket.io 的 .socket(socket.id).emit(event,message) 将消息发送到特定的 socket.id 来实现私有通信,但是这种方法的问题在于,每次用户打开一个新选项卡时,都会为该新连接生成一个新的 socket.id.

Currently, I've achieved private communication by sending the messages to a particular socket.id, using the socket.io's .socket(socket.id).emit(event,message), but the problem with that approach is that every time the user opens a new tab, a new socket.id is generated, for that new connection.

我的问题是:sails.js 是否有助于向特定用户会话而不是一堆套接字 ID 发送事件(使用 socket.io)?这些技术有可能吗?

And my question is: does sails.js facilitates a way of emitting events (using socket.io) to an specific user session instead of a bunch of socket ids? is it possible with these technologies?

所以我只能发送一次事件,并确保在聊天应用程序当前打开的所有选项卡中都收到它.

So I can send the event only once, and making sure it is received in all the tabs where the chat app is currently open.

提前致谢.

推荐答案

Sails 用一些有用的 pubsub 方法扩充了它的所有模型.每当您通过套接字请求(例如 socket.get('/user/123') )找到模型时,请求套接字就会自动订阅有关该模型的所有消息.然后,您可以调用 User.message(123, data) 向该用户的所有连接的套接字发送消息.

Sails augments all of its models with some useful pubsub methods. Whenever you find a model via a socket request (e.g. socket.get('/user/123') ) the requesting socket is automatically subscribed to all messages about that model. You can then call User.message(123, data) to send a message to all of the connected sockets for that user.

发布订阅方法的文档在这里.

Docs for the pubsub methods are here.

这篇关于使用 socket.io 和 Sails.js 发送会话特定的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆