为什么Autobahn Twisted Websocket服务器没有完成与javascript客户端的握手?

时间:2016-08-24 15:02:02

标签: javascript python websocket twisted autobahn

我正在尝试使用扭曲的websocket服务器并将其连接到localhost上的javascript客户端,而不是通过网络。服务器和客户端看到对方但无法完成握手。是的,由于系统要求,我正在使用txWS提供的Hixie-76包装器。

我难以理解为什么他们无法连接?

版本:高速公路0.16,扭曲0.16.3

这是我想要实现的一个实际例子: https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo使用server.py和client.html

日志:

2016-08-24 15:44:33+0100 [-] Log opened.
2016-08-24 15:44:33+0100 [-] WebSocketServerFactory (WebSocketFactory) starting on 8080

2016-08-24 15:44:33+0100 [-] Starting factory <autobahn.twisted.websocket.WebSocketServerFactory object at 0x00000000031DB7F0>

2016-08-24 15:44:33+0100 [-] Starting factory <txws.WebSocketFactory instance at 0x0000000002FA82C8>

2016-08-24 15:44:34+0100 [MyServerProtocol (WebSocketProtocol),0,127.0.0.1] Starting HyBi-00/Hixie-76 handshake

2016-08-24 15:44:34+0100 [MyServerProtocol (WebSocketProtocol),0,127.0.0.1] Completed HyBi-00/Hixie-76 handshake

2016-08-24 15:44:39+0100 [-] WebSocket connection closed:
2016-08-24 15:44:39+0100 [-] False
2016-08-24 15:44:39+0100 [-] 1006
2016-08-24 15:44:39+0100 [-] connection was closed uncleanly (peer did not finish (in time) the opening handshake)

Python类:

from txws import WebSocketFactory
from twisted.internet import reactor
from twisted.python import log
from autobahn.twisted.websocket import WebSocketServerFactory
from autobahn.twisted.websocket import WebSocketServerProtocol

import json
import sys

class MyServerProtocol(WebSocketServerProtocol):

    def onMessage(self, payload, isBinary):
        print "Message Received!!!!"
        msg = json.dumps({'status':'PLEASE WORK'})
        self.sendMessage(msg, isBinary=False)

    def onClose(self, wasClean, code, reason):
        print "WebSocket connection closed: "
        print str(wasClean)
        print str(code)
        print str(reason)

def make_server():
    print 'Making ws server'
    log.startLogging(sys.stdout)
    factory = WebSocketServerFactory("ws://127.0.0.1:8080")
    factory.protocol = MyServerProtocol
    reactor.listenTCP(8080, WebSocketFactory(factory)) #txWS WebSocketFactory wrapper
    reactor.run()

使用Javascript:

function ConnectWebSocket() { 
    websocket = new WebSocket('ws://127.0.0.1:8080');
    websocket_open = true;

    websocket.onopen = function(e) { 
        console.log('opened');
        console.log(e);
        websocket.send('slow test'); 
    };
    websocket.onclose = function(e) { 
       console.log("Connection closed.");
       websocket_open = false;
       websocket = null;
        ConnectWebSocket();
    };
    websocket.onmessage = function(e) { 
        console.log('message');
        console.log(e.data);
    };
    websocket.onerror = function(e) { 
        console.log('error');
        console.log(e);
    };
}
ConnectWebSocket();

1 个答案:

答案 0 :(得分:0)

好的,我发现了这个问题。正如迈克尔S所暗示的那样,它可能是在Hixie-76封装器中。开发人员必须让协议随着时间推移而不再有效。我可以通过在代码中追溯来确认这一点。我会将它报告给txWS的开发。

我找到了Hixie-76问题的替代解决方案。我在https://github.com/gleicon/txwebsockets将包装器切换到txWebSockets。不是优雅的解决方案,但它现在有效。