简体   繁体   English

是否可以将Websocket连接发送到客户端?

[英]Is it possible to send a Websocket connection to a client?

folks! 乡亲!

I have the following scenario: 我有以下情况:

  • I'm running a Tornado server with a WebSocketHandler that handles open, on_message and on_close events on a websocket: 我正在运行带有WebSocketHandler的Tornado服务器,该服务器可处理websocket上的open,on_message和on_close事件:

      class WebSocketHandler(tornado.websocket.WebSocketHandler): def open(self): (...) def on_message(self, message): (...) def on_close(self): (...) 
  • I have a view where the client opens a new WebSocket and sets the "onmessage" event handler: 我有一个视图,其中客户端打开一个新的WebSocket并设置“ onmessage”事件处理程序:

      window.onload = function() { ws = new WebSocket("ws://192.168.0.51:8080/websocket"); ws.onmessage = function(e) { console.log(e.data); }; } 
  • I would like to change this example a little bit to "reuse" a websocket connection if the user, let's say, opens a new tab in his browser but he has already an opened websocket at the server side. 如果用户在浏览器中打开一个新标签,但他已经在服务器端打开了一个websocket,我想稍微修改一下这个示例以“重用”一个websocket连接。

    I'm intending to do this by sending a user unique id when making the first request to the server and checking the response, before opening a socket. 我打算通过向服务器发出第一个请求时发送用户唯一ID并在打开套接字之前检查响应来实现此目的。

    Question is : If the Tornado server detects that the user already has an opened connection (websocket), is it possible to send to that user(client) a reference to that connection in order to enable the client side code to use it instead of creating a new one? 问题是 :如果Tornado服务器检测到用户已经打开了连接(Websocket),是否可以向该用户(客户端)发送对该连接的引用,以使客户端代码可以使用该连接而不是创建一个新的?

Thanks in advance! 提前致谢!

In principle, this can be achieved by opening the WebSocket connection from a shared background Web worker. 原则上,这可以通过从共享的后台Web worker中打开WebSocket连接来实现。

Opening WebSocket connections from Web workers is not supported (yet) by all browsers. 并非所有浏览器都支持(到目前为止)从Web Worker打开WebSocket连接。 I know it works with dedicated Web workers on Chrome and IE11, and it does not work with Firefox. 我知道它适用于Chrome和IE11上的专用Web工作者,并且不适用于Firefox。

I don't know which browsers support opening WebSocket connections from shared Web workers. 我不知道哪些浏览器支持从共享 Web Worker打开WebSocket连接。

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

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