简体   繁体   English

如何使用Websockets发送旧消息

[英]How to send old messages with Websockets

I've got a working Websockets example, where clients receive messages from the server. 我有一个工作的Websockets示例,客户端从服务器接收消息。

I'm not sure how I should send old messages to clients when they connect. 我不确定如何在连接时向客户端发送旧消息。

Example: 例:

  • Each client supplies their name when they connect 每个客户端在连接时都会提供其名称
  • The server responds with "[name] just connected" (to all clients) 服务器响应“[name] just connected”(对所有客户端)
  • Any new clients would NOT get these messages 任何新客户都不会收到这些消息

I'm wondering if there's any way clients can receive old messages (either all of them, or messages in the last 5 minutes would be acceptable). 我想知道客户是否有任何方式可以接收旧消息(无论是全部消息,还是最近5分钟内的消息都可以接受)。

I suspect I may have to capture this information myself, store it somewhere (like a database) and send the messages to new clients myself. 我怀疑我可能必须自己捕获这些信息,将其存储在某个地方(如数据库)并自己将消息发送给新客户。 Is that right, or am I missing something? 是的,或者我错过了什么?

If anyone has pseudo code, or a link to an example of how others have implemented this, that would be handy. 如果有人有伪代码,或链接到其他人如何实现这一点的示例,那将是很方便的。

You will have to capture it by your own and store it on server... once user connects you will have to name that data to all connected clients and the messages which you have stored back to the user who has connected. 您必须自己捕获它并将其存储在服务器上...一旦用户连接,您必须将该数据命名为所有连接的客户端,并将已存储的消息返回给已连接的用户。 So, you will have to code to broadcast the data to users 因此,您必须编写代码以向用户广播数据

By the way what are you using server side? 那么你使用服务器端的方式是什么? (Node, Erlang , etc) (节点,Erlang等)

You can check following link if you are using node.js 如果您使用node.js,可以检查以下链接

http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial

You could do something like this: 你可以这样做:

  1. Each message should have an id -> muid ( Message Unique ID ) 每条消息都应该有一个id - > muid消息唯一ID
  2. Each time a client send sa message, it gets an ACK from the server along with the muid for the sent message. 每次客户端发送sa消息时,它都会从服务器获得ACK以及发送消息的muid
  3. Each time a new message is received in the server side, a muid is assigned, sent with the ACK and also sent with the message to every connected user. 每次在服务器端接收到新消息时,都会分配一个muid ,与ACK一起发送,并随消息一起发送给每个连接的用户。 This way the view will be able to present, for every user, the same sequence at some point in the time. 这样,视图将能够为每个用户在某个时间点呈现相同的序列。
  4. Each time a new user connects it sends the last muid it has received so the server knows where this user stopped receiving messages. 每次新用户连接时,都会发送它收到的最后一个muid ,以便服务器知道该用户停止接收消息的位置。 The server could then send as many old messages as you want, depending on the kind of storage you implement: 然后,服务器可以根据您的需要发送尽可能多的旧邮件,具体取决于您实现的存储类型:
    1. Full history: I would recommend a database storage with proper indexing 完整历史记录:我建议使用正确的索引编制数据库存储
    2. Last N messages: Depending on the size of N you could simply store the last N messages in a fixed size Array and send them, all or the needed chunk, on each reconnection. 最后N条消息:根据N的大小,您可以简单地将最后N条消息存储在固定大小的数组中,并在每次重新连接时发送它们,所有或所需的块。 Keep in mind that this will consume memory so, storing last 1024 messages for 1024 different chats would eat quite a bit of memory, specially if messages are of unlimited size. 请记住,这将消耗内存,因此,为1024个不同的聊天记录存储最后1024条消息将占用相当多的内存,特别是如果消息的大小不限。

Hope it helps 希望能帮助到你

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

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