“WebSocket 握手期间出错:状态行无效”
Posted
技术标签:
【中文标题】“WebSocket 握手期间出错:状态行无效”【英文标题】:"Error during WebSocket handshake: Invalid status line" 【发布时间】:2015-09-06 16:11:50 【问题描述】:连接到我的 WebSocket 时,我在客户端连接后立即收到上述错误。
2015-09-06 12:01:12-0400 [-] reactor class: twisted.internet.epollreactor.EPollReactor.
2015-09-06 12:01:12-0400 [-] ServerFactory starting on 1025
2015-09-06 12:01:12-0400 [-] Starting factory <twisted.internet.protocol.ServerFactory instance at 0x1f92f80>
2015-09-06 12:01:28-0400 [-] Got new client!
2015-09-06 12:01:28-0400 [-] received 'GET / HTTP/1.1'
2015-09-06 12:01:28-0400 [-] received 'Host: ip:1025'
2015-09-06 12:01:28-0400 [-] received 'Connection: Upgrade'
2015-09-06 12:01:28-0400 [-] received 'Pragma: no-cache'
2015-09-06 12:01:28-0400 [-] received 'Cache-Control: no-cache'
2015-09-06 12:01:28-0400 [-] received 'Upgrade: websocket'
2015-09-06 12:01:28-0400 [-] received 'Origin: http://server-ip.net'
2015-09-06 12:01:28-0400 [-] received 'Sec-WebSocket-Version: 13'
2015-09-06 12:01:28-0400 [-] received 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/45.0.2454.85 Safari/537.36'
2015-09-06 12:01:28-0400 [-] received 'Accept-Encoding: gzip, deflate, sdch'
2015-09-06 12:01:28-0400 [-] received 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4'
2015-09-06 12:01:28-0400 [-] received 'Sec-WebSocket-Key: Au1JIvJG0A2Jep3+wqCPRg=='
2015-09-06 12:01:28-0400 [-] received 'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits'
2015-09-06 12:01:28-0400 [-] received ''
2015-09-06 12:01:28-0400 [-] Lost a client!
在客户端它看起来像这样:
WebSocket connection to 'ws://server-ip:1025/' failed: Error during WebSocket handshake: Invalid status line
我一直在尝试做的事情:
我关注this tutorial 创建了一个实时聊天(与 Django)。我使用的 chatserver.py 直接来自他们的文档:https://twistedmatrix.com/documents/current/_downloads/chatserver.py。其余的(jQuery 脚本等)与该教程中的一样。
【问题讨论】:
【参考方案1】:MyChat.lineReceived
将消息发送给每个连接的客户端包括本身。我希望这意味着它会将收到的所有标头回显给 WebSocket 客户端,这可能不是它所期望的。
您链接到的 ferritfarmer 教程通过将 MyChat 的工厂包装在 WebsocketResource
中来处理此问题,这是对您直接从普通 Twisted 示例链接到的 chatserver.py
的修改。
我相信 WebsocketResource 来自教程链接到的 Twisted 的特定开发分支。不幸的是,该分支似乎停滞不前。我听说Autobahn 的 WebSocket 实现是个不错的选择。
【讨论】:
以上是关于“WebSocket 握手期间出错:状态行无效”的主要内容,如果未能解决你的问题,请参考以下文章