我使用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
答案 0 :(得分:1)
您在上面看到的回溯是由于Autobahn 0.7.0引入的一个错误,并在0.7.4中修复。
然而,该错误可能不是您实际问题的原因:回溯显示Autobahn正在尝试呈现纯HTML服务器状态页面 - 当它收到的HTTP请求不包含升级到Websocket时它会这样做头。您的代理可能尚未配置为正确转发WebSocket。
您可能遇到的另一件事:Autobahn的WebSocketServerFactory
提供了设置externalPort
的选项。这应该设置为代理的TCP / IP端口,在该端口下它接受WebSocket连接。这是必需的,因为Autobahn将(符合WebSocket规范)检查WebSocket打开HTTP请求主机和端口是否与其运行的主机和端口匹配(如果它不同,则纾困)。提到的选项允许您覆盖该行为。
答案 1 :(得分:1)
尝试使用安全的websockets(wss),因为代理通常不支持非安全的websockets。