高速公路/扭曲 "Unexpected response code 200"

AutoBahn / Twisted "Unexpected response code 200"

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

这是我服务器中的代码 (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

您在 /ws 安装了 WebSocketResource。尝试更改您的客户端以反映这一点:

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