2017-03-04 153 views
1

我正在嘗試用AutoBahn [twisted]運行Python WebSocket服務器。AutoBahn/Twisted「意外的響應代碼200」

這裏是我的服務器代碼(的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 

回答

0

您將WebSocketResource安裝在/ws處。嘗試改變您的客戶以反映此:

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