0

我正在使用 nginx 从 9000 到 80 端口代理我的测试 websocket,并且我的所有测试都可以,直到客户端位于 web 代理之后,然后握手过程失败,问题可能出在哪里?在此先感谢您的时间。

我收到这些错误:
客户:

WebSocket connection to 'ws://myserver/ws/' failed: Error during WebSocket handshake: Unexpected response code: 502 autobahn.min.js:62

服务器:

2014-01-10 19:51:22-0300 [PubSubServer1,19,127.0.0.1] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
        return callWithContext({"system": lp}, func, *args, **kw)
      File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
        return context.call({ILogContext: newCtx}, func, *args, **kw)
      File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
        return self.currentContext().callWithContext(ctx, func, *args, **kw)
      File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
        return func(*args,**kw)
    --- <exception caught here> ---
      File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite
        why = selectable.doRead()
      File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 199, in doRead
        rval = self.protocol.dataReceived(data)
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/twisted/websocket.py", line 77, in dataReceived
        self._dataReceived(data)
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 1270, in _dataReceived
        self.consumeData()
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 1303, in consumeData
        self.processHandshake()
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 2819, in processHandshake
        self.sendServerStatus()
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 3276, in sendServerStatus
        self.sendHtml(html)
      File "/usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg/autobahn/websocket/protocol.py", line 3219, in sendHtml
        response += "Content-Length: %d\x0d\x0a" % len(raw)
    exceptions.NameError: global name 'raw' is not defined

我的测试服务器:

import sys
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import listenWS
from autobahn.wamp import WampServerFactory, \
                          WampServerProtocol

class PubSubServer1(WampServerProtocol):

   def onSessionOpen(self):

      self.registerForPubSub("http://test.com/test")

if __name__ == '__main__':

   log.startLogging(sys.stdout)

   factory = WampServerFactory("ws://localhost:9000", debugWamp = 'debug',externalPort=80)
   factory.protocol = PubSubServer1
   factory.setProtocolOptions(allowHixie76 = True)
   listenWS(factory)

   #reactor.listenTCP(9000, factory)
   reactor.run()

命令:

~$ python /home/my/wsbroker.py 
/usr/local/lib/python2.7/dist-packages/zope.interface-4.0.5-py2.7-linux-x86_64.egg/zope/__init__.py:3: UserWarning: Module twisted was already imported from /usr/lib/python2.7/dist-packages/twisted/__init__.pyc, but /usr/local/lib/python2.7/dist-packages/autobahn-0.7.3-py2.7.egg is being added to sys.path

已编辑:
握手失败的调试信息(使用 Web 代理的客户端):http
://pastebin.com/aN4ppA2e 已完成握手的调试信息(没有代理的客户端): http: //pastebin.com/5rXREY2q

4

2 回答 2

1

您在上面看到的回溯是由于 Autobahn 0.7.0 引入并在 0.7.4 中修复的错误。

但是,该错误可能不是您实际问题的原因:回溯显示 Autobahn 正在尝试呈现纯 HTML 服务器状态页面 - 当它收到的 HTTP 请求不包含升级到 Websocket 标头时,它会这样做。您的代理可能(尚未)配置为正确转发 WebSocket。

您可能会遇到的另一件事是:WebSocketServerFactoryof Autobahn 提供了一个选项来设置externalPort. 这应该设置为代理的 TCP/IP 端口,在该端口下它接受 WebSocket 连接。这是必需的,因为 Autobahn 将(符合 WebSocket 规范)检查 WebSocket 打开 HTTP 请求主机和端口是否与它运行的主机和端口匹配(如果不同,则退出)。提到的选项允许您覆盖该行为。

于 2014-01-11T23:20:42.443 回答
1

尝试使用安全 websockets (wss),因为代理通常不支持非安全 websockets。

于 2014-01-12T20:59:02.257 回答