使用 tornado websockets 传递消息

Posted

技术标签:

【中文标题】使用 tornado websockets 传递消息【英文标题】:message passing with tornado websockets 【发布时间】:2012-05-03 20:28:59 【问题描述】:

我正在使用 tornado websockets 来更新页面上的一些信息。如果用户更改屏幕上的某些内容,我希望将这些更改显示给所有其他具有活动连接的用户。我可以让 javascript 将消息发送到服务器,但我不知道如何将该消息发送回客户端。这是javascript和python

$(document).ready(function () 
    var ws = new WebSocket("company website, I know this works");

    ws.onopen = function () 
        console.log("websocket engage");
    ;

    ws.onmessage = $(".column li").on("mouseup", function (evt) 
        pid = $(this).attr("id");
        oldCid = $(this).parent().parent().attr("id");
        newCid = $(this).parent().attr("id");
        message = pid + " " + oldCid + " " + newCid;
        ws.send(message);
    );

    ws.onclose = function (evt) 
        console.log("connection closed");
    ;

    ws.writemessage = function (evt) 
        alert(evt.data);
    ;

);

这里是python代码:

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)

change = "original"

listeners = []

class WSHandler(tornado.websocket.WebSocketHandler):
  def open(self):
    print "opened a new websocket"
    listeners.append(self)
    print listeners

  def on_message(self, message):
     #self.write_message(u"You Said: " + message)
     print ("in on_message " + message)
     change = message
     #self.write_message(message)

  def on_close(self):
    print 'connection closed'
    listeners.remove(self)

  def write_message(self, message):
    print ("in write message " + change)
    self.write_message(change)

def main():
    #tornado.options.parse_command_line()
    application = tornado.web.Application([
(r'/ws', WSHandler),
 ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

【问题讨论】:

【参考方案1】:

在 python 代码中,您已将 /ws 映射到 WSHandler 类,但在创建 WebSocket 对象时,您需要在 javascript 代码中执行此操作:

1)

var ws=new WebSocket("ws://192.168.3.33:8888/ws"); 
/* 
  if 192.168.3.33 is the server IP and 8888 is the port, the server is serving on. 
  So object is mapped to r"/ws". so now the server can identify the request 
*/

而不是:

var ws = new WebSocket("company website, I know this works");

2)WebSocket的onmessage()事件发生在服务器发回东西时。

所以 javascript 代码将如下所示:

$(document).ready(function () 
var ws=new WebSocket("ws://192.168.3.33:8888/ws");

ws.onopen = function () 
    console.log("websocket engage");
;

ws.onmessage = function(evt)
    //the received content is in evt.data, set it where you want
;

ws.onclose = function () 
    console.log("connection closed");
 ;

$(".column li").on("mouseup")  //the function  that sends data

    pid = $(this).attr("id");
    oldCid = $(this).parent().parent().attr("id");
    newCid = $(this).parent().attr("id");
    message = pid + " " + oldCid + " " + newCid;
    ws.send(message);   
;
);

【讨论】:

【参考方案2】:

重新定义:

  def on_message(self, message):
     print ("in on_message " + message)
     for w in listeners:
        w.write_message(message)

这将向所有连接的客户端发送“消息”。

另外,我建议删除您的 self.write_message 版本。

【讨论】:

以上是关于使用 tornado websockets 传递消息的主要内容,如果未能解决你的问题,请参考以下文章

使 Tornado websocket 处理程序线程安全

websocket[tornado] 的单个实例可以处理不同的请求吗?

Django、websockets、Tornado,如何让它们通信?

tornado websocket

tornado 只能升级到 websocket 错误

如何在 Tornado 中创建多个 websocket 聊天?