发送消息时未调用WebSocket onmessage()

WebSocket onmessage() not being called when messages are sent

本文关键字:WebSocket onmessage 调用 消息      更新时间:2023-09-26

我使用的是0.4.10高速公路(https://github.com/oberstet/Autobahn)作为WebSocket服务器向Google Chrome扩展发送消息。我可以使用WebSocket()打开和关闭连接,但当我调用autobahn.websocket.WebSocketServerProtocol.sendMessage()时,消息似乎已发送,但在关闭连接之前不会传递。

WebSocketServerProtocol的sendMessage()(源自WebSocketProtocol)的api可以在这里找到:http://www.tavendo.de/autobahn/doc/python/websocketprotocol.html#autobahn.websocket.WebSocketProtocol

以前有人经历过这个问题吗?

我在客户端的代码是(js):

ws = new WebSocket('ws://localhost:4444');
ws.onmessage = function(event) {
  console.log('hii');
}

在服务器(python)上。。。

#json is a string object
def sendEvent(self, json):
    print 'to', self.peerstr
    self.sendMessage(json, sync=True)

Autobahn和我的Chrome版本(17.0.963.46)(根据我从标题和文档中得到的信息)都使用了WebSocket草案协议的第13版。

原来这是一个线程问题,一些线程阻塞了扭曲的反应器。

请参阅:http://groups.google.com/group/autobahnws/browse_thread/thread/6bf0c43ec169efc3#http://twistedmatrix.com/documents/current/core/howto/threading.html

Autobahn适用于Chrome(测试版本为v19.Canary)。

你能试试吗

https://github.com/oberstet/Autobahn/blob/master/demo/broadcast/broadcastrongerver.py

演示,看看你是否有一个普遍的问题?

如果运行,请将您的扩展指向该演示服务器。。它会每秒向你发送1次。

您还可以通过将工厂行更改为类似的代码来启用调试输出

https://github.com/oberstet/Autobahn/blob/master/demo/echo/echo_server_with_logging.py#L50

另外2条注释:

  • 您不需要sync=True选项(它实际上是一个高级选项…主要用于高速公路WS测试套件)

  • 你可能想加入http://groups.google.com/group/autobahnws。。更快地得到答案。。我在这里偶然发现了你的Q

披露:我是《高速公路》的作者,为塔文多工作。