AutoBahn / Twisted“意外响应代码200”

时间:2017-03-04 03:24:01

标签: javascript python-3.x websocket twisted autobahn

我正在尝试使用AutoBahn [twisted]运行Python WebSocket服务器。

以下是我服务器中的代码( Python ):

*various imports*

class webSocket(WebSocketServerProtocol):
def onConnect(self, request):
    print("some request connected {}".format(request))

def onMessage(self, payload, isBinary):
    self.sendMessage("message received")

log.startLogging(sys.stdout)
root = File(".")

factory = WebSocketServerFactory(u"ws://0.0.0.0:8080")
factory.protocol = webSocket

resource = WebSocketResource(factory)

root.putChild(u"ws", resource)

site = Site(root)
reactor.listenTCP(8080, site)
reactor.run()

以下是我的客户端中的代码( Javascript ):

window.webSocket = new WebSocket("ws://localhost:8080");
      webSocket.onclose=function(event){
        console.log(event)
        setTimeout(connectToSocket, 1000);
      }

然而,Javascript将继续抛出以下错误(并尝试重新连接):

WebSocket connection to 'ws://localhost:8080/' failed: Error during WebSocket handshake: Unexpected response code: 200

1 个答案:

答案 0 :(得分:0)

您已将WebSocketResource加载到/ws。尝试更改您的客户端以反映这一点:

window.webSocket = new WebSocket("ws://localhost:8080/ws");
...