使用 websockets 的私人消息 [英] private message using websockets

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

问题描述

我们如何使用 Websockets(和 PHP)实现私有消息系统(客户端到客户端)?

How can we implement a private message system (client to client) using Websockets (and PHP)?

据我所知,服务器广播消息,所有客户端都可以通过 connection.onmessage 事件处理程序接收消息.我想知道是否有办法将消息仅发送给 websockets 中的目标用户?

From what I understood, the server broadcasts the message and all of the clients can receive the message through connection.onmessage event handler. I was wondering if there is a way to send the message to only a targeted user in the websockets?

推荐答案

当客户端发送消息 (ws.send( message );) 时,您的 WebSocket 服务器将收到该消息.然后您将其发送到的套接字完全取决于您的服务器代码 - 您的实现.

When a client sends a messages (ws.send( message );) your WebSocket server will receive the message. The sockets that then you send that on to is determined entirely by your server code - your implementation.

要创建一对一聊天,您需要一种仅在聊天中涉及的两个客户端之间路由数据的方法.您还需要一种验证方式,即只有这两个客户端才能接收该信息.

To create a one-to-one chat you need a way of routing data between just the two clients involved in the chat. You also need a way of authenticating that only those two clients can receive that information.

WebSocket 框架倾向于提供额外的 PubSub 层,例如Pusher(我以前为他工作过)使用 channels 执行此操作.除了主题"和主题"之外,您还会发现类似的术语.

WebSocket frameworks tend to provide an additional PubSub layer e.g. Pusher (who I previously worked for) do this using channels. You will find similar terminology along with 'subjects' and 'topics'.

一旦您有一种在两个客户端之间路由数据(聊天消息)的方法,您就需要考虑对订阅进行身份验证.可以通过这个问题找到更多信息,该问题询问如何在两个用户之间创建私人频道.这个问题是关于 Ruby 的,但适用于任何技术.

Once you have a way of routing data (chat messages) between just two clients you then need to consider authenticating the subscription. More information can be found on that via this question which asks How do I create Private channel between two user. The question is about Ruby but is applicable to any technology.

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

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