使用sockjs龙卷风的私人消息 [英] Private messaging using sockjs-tornado

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

问题描述

我已经使用sockjs-tornado实现了聊天功能,可以将消息存储在RethinkDB中。

I have implemented chat feature using sockjs-tornado and could store the messages in RethinkDB.

请问如何建立私人信息sockjs龙卷风? (我的意思是私人谈话/一对一)

Could you please help me on how do I establish private channel for messaging in sockjs-tornado ? (I mean Private conversation / one to one)

以下是我的服务器端代码中的on_message函数 -

Below is the on_message function in my server side code -

def on_message(self, message):
    str=message
    mg=str.split('#:#')
    sender=1 # This is the sender user id
    receiver=2 #This is the receiver user id - I need to implement session variables to have these id's so that I can use it here this way
    ts=r.expr(datetime.now(r.make_timezone('00:00')))
    connection = r.connect(host="192.x.x.x")
    r.db("djrechat").table('events').insert({"usrs":mg[0],"msg":mg[1],"tstamp":ts,"snder":sender,"rcver":receiver}).run(connection)
    log.info(message)
    self.broadcast(self.participants, '{} - {}'.format(self.stamp(),message))

目前,这是向所有连接的客户端进行广播。
可能我应该有一个频道ID,并只发送消息到两个客户端将具有相同的频道ID,但如何实现它或有什么更好的解决方案吗?

Currently this is broadcasting to all the clients connected. May be I should have a channel id and to send message only to the two clients which will have the same channel id, but how do I implement it or is there any better solution for this?

在客户端,我有以下javascript -

At client side, I have below javascript -

      function connect() {
        disconnect();
        conn = new SockJS('http://localhost:8080/chat', ['websocket','xhr-streaming','iframe-eventsource','iframe-htmlfile','xhr-polling','iframe-xhr-polling','jsonp-polling']);
        //log('Connecting.....');
        conn.onopen = function() {
        //  log('Connected. (' + conn.protocol + ')');
        log('Connected.');
        };

        conn.onmessage = function(e) {
          log(e.data);
        };

        conn.onclose = function() {
          log('Disconnected.');
          conn = null;
        };
      }

使用python 3.4 - Django 1.8.4和Rethinkdb

Am using python 3.4 - Django 1.8.4 and Rethinkdb

推荐答案

我的回答假定当前解决方案中的所有客户端都连接到具有用于聊天消息广播的频道名称的SockJS服务器(或接近这种情况的东西)。我进一步假设当用户从客户端发送消息时的往返行为是:

My answer makes the assumption that all clients in the current solution connect to a SockJS server with a channel name that is used for broadcasts of the chat messages (or something close to this scenario). I further assume that the round-trip when a user sends a message from the client is:

[Sender (Client)] ------- Message (POST) --> [App Server] --- Message --> [Database]
                                                  |
                                               Message
                                                  v
[Receiver(s) (Client)] <-- Message (WS) -- [SockJS Server]

这个问题有多种解决方案。我只会概述我认为最简单和最强大的一个:

There are multiple solutions to this problem. I will only outline the one I think is simplest and most robust here:


  • 让每个用户订阅一个 -user (私人)频道到聊天中的广播频道。如果您实现此解决方案,则只有其他更改是以使应用服务器知道必须仅发送的私人消息到接收者的私人频道。

  • Let every user subscribe to a per-user (private) channel in addition to the broadcast-channel in the chat. If you implement this solution, the only additional change is to make the app server aware of private messages which must be sendt only to the receiver's private channel.

另一个稍微复杂和不成熟的解决方案将是创建 ad hoc 渠道每个私人配对在需要时。但是,当用户A时,您将如何获得用户B 订阅(新创建)的私人频道想与用户B 聊天?一种方法是广播请求 User B 连接到该频道,但会告诉所有其他客户端(代码级别)关于这个私人聊天将发生;如果任何客户遭到入侵,该信息可能会被滥用,例如窃听或破坏私人聚会。

Another, slightly more complex, and inelegant solution would be to create ad hoc channels for each private pair when needed. However, how would you get User B to subscribe to the (newly created) private channel when User A wants to chat with User B? One way would be to broadcast a request for User B to connect to that channel, but that would tell all the other clients (at the code level) about this private chat that will take place; and if any of those clients are compromised, that information can be misused for, for example, eavesdropping or crashing the private party.

我也想提一下,因为我写了我的 sockjs - 实施房间的例子答案,我已经改变了我自己的架构,在前端使用 SockJS ,但是使用 RabbitMQ 使用 Web-STOMP插件在后端。这样,

I would also like to mention that since I wrote my sockjs - example of implementing rooms answer, I have changed my own architecture to (still) use SockJS in the front-end, but with RabbitMQ using the Web-STOMP plugin on the back-end. That way,


  1. 客户端仍然使用 sockjs-client stomp-websocket 在浏览器中

  2. 可以删除所有用于处理前端多路复用的后端代码

  3. (基于RabbitMQ)的后端是快速非常强大

  4. 客户端向后端应用服务器发送消息,而后者则

  1. the client still uses sockjs-client with stomp-websocket in the browser,
  2. all of the back-end code to handle front-end multiplexing could be removed,
  3. the (RabbitMQ-based) back-end is fast and really robust,
  4. the client POSTs a message to the back-end app server, which in turn

  1. 在数据库中保留消息,

  2. 充当RabbitMQ服务器的客户端,并且(1)向所有连接的用户广播消息,或者如果消息是私有 (2)通过收件人自己的私人频道将消息发送给单个收件人,如上所述,如图所示

  1. persists the message in the database, and
  2. acts as a client of the RabbitMQ server and either (1) broadcasts the message to all connected users, or if the message is private (2) sends the message to a single recipient over the recipient's own private channel, as sketched out in the above suggested solution.


全新的解决方案放在HAProxy之后,终止HTTPS和SSL / TLS加密的WebSocket连接。

The whole new solution is placed behind HAProxy which terminates HTTPS and SSL/TLS-encrypted WebSocket connections.

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

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