socket.io私人消息 [英] socket.io private message

查看:89
本文介绍了socket.io私人消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找运气不佳的网络.我试图弄清楚如何从一个用户向另一个用户发送私人消息.有很多片段,但是我不确定客户端/服务器之间的交互.如果我有要发送到的套接字的ID,该如何将其发送到服务器,以及如何确保服务器仅将消息发送到该接收者的套接字?

I've beeen scouring the Net with no luck. I'm trying to figure out how to send a private message from one user to another. There are lots of snippets, but I'm not sure about the client/server interaction. If I have the ID of the socket I want to send to, how do I send it to the server, and how do I ensure the server only sends the message to that one receiver socket?

有没有人知道的教程或演练?

Is there a tutorial or walkthrough that anyone knows of?

推荐答案

无需教程. Socket.IO常见问题解答对此非常简单:

No tutorial needed. The Socket.IO FAQ is pretty straightforward on this one:

socket.emit('news', { hello: 'world' });

编辑:人们在询问以后如何获取该socket对象时,人们会链接到该问题.没有必要.当新的客户端连接时,请在要保留用户信息的任何对象上保存对该套接字的引用.我的评论如下:

Folks are linking to this question when asking about how to get that socket object later. There is no need to. When a new client connects, save a reference to that socket on whatever object you're keeping your user information on. My comment from below:

在脚本顶部的某个地方,设置一个对象来保存用户的信息.

In the top of your script somewhere, setup an object to hold your users' information.

var connectedUsers = {};

在您的.on('connection')函数中,将该套接字添加到新对象. connectedUsers[USER_NAME_HERE] = socket;然后,您可以在以后轻松地将其检索. connectedUsers[USER_NAME_HERE].emit('something', 'something');

In your .on('connection') function, add that socket to your new object. connectedUsers[USER_NAME_HERE] = socket; Then you can easily retrieve it later. connectedUsers[USER_NAME_HERE].emit('something', 'something');

这篇关于socket.io私人消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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