客户端连接后立即断开,没有错误消息?

3
新手尝试使用 Twisted 框架,并进行实验。我正在尝试使用 twisted.application 和 protocols.basic.LineReceiver 来设置一个简单的 WebSocket,用于消息系统。
问题:Twisted 应用程序连接客户端后,立即断开连接?
当尝试连接时,客户端日志如下:
WebSocket 受您的浏览器支持! Firefox 无法在 ws://127.0.0.1:1025/ 上与服务器建立连接。 连接已关闭...
客户端尝试连接时,服务器日志如下:
2019年2月24日17:49:24+0000 [stdout#info] 收到新客户端! 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'GET / HTTP/1.1' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Host: 127.0.0.1:1025' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Accept: /' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Accept-Language: en-US,en;q=0.5' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Accept-Encoding: gzip, deflate' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Sec-WebSocket-Version: 13' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Origin: null' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Sec-WebSocket-Extensions: permessage-deflate' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Sec-WebSocket-Key: /gN0KPBQZTU498eQBdTV2Q==' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'DNT: 1' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Connection: keep-alive, Upgrade' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Pragma: no-cache' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Cache-Control: no-cache' 2019年2月24日17:49:24+0000 [stdout#info] 接收到 b'Upgrade: websocket' 2019年2月24日17:49:24+0000 [stdout#info] 失去一个客户端! 服务器代码:
"""The most basic chat protocol possible.

run me with twistd -y chatserver.py, and then connect with multiple
telnet clients to port 1025
"""
from __future__ import print_function

from twisted.application import service, internet
from twisted.internet import protocol, reactor
from twisted.protocols import basic


class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print("Got new client!")
        self.factory.clients.append(self)

    def connectionLost(self, reason):
        print("Lost a client!")
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        print("received", repr(line))
        for c in self.factory.clients:
            c.message(line)

    def message(self, message):
        self.transport.write(message + b'\n')


factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []

application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)

使用twistd -y chatserver.py运行它

简单客户端代码: (此代码可连接到本地运行的pywebsocket

<script>
    console.log("started");
    window.chat = {};
    //Instantiate a websocket client connected to our server
    chat.ws = new WebSocket("ws://127.0.0.1:1025");
    
    chat.ws.onopen = function () {
        console.log("Connected to chat.")
    };

    chat.ws.onclose = function () {
        console.log('Connection closed');
    };
</script>

我正在使用 Twisted 18.9.0,Python 3.7.1,并在MacOS上运行。 有人知道我做错了什么导致无法保持连接吗?
1个回答

1
你正在运行一个简单的TCP回显服务器。它接受TCP连接并将客户端发送的内容回显给它们。
你正在运行一个WebSocket客户端。它打开一个TCP连接,然后开始使用WebSocket协议与服务器交流(从基于HTTP的握手开始)。它期望获得WebSocket协议响应来完成这个握手。
TCP回显服务器将客户端的WebSocket握手数据发回给它。这不是正确的WebSocket握手响应。WebSocket客户端正确地断定该服务器不是WebSocket服务器并断开连接。
查看类似https://crossbar.io/autobahn/之类的库,用于处理WebSockets。甚至可以在文档中找到一个示例WebSocket回显服务器,https://github.com/crossbario/autobahn-python/tree/9d65b508cc108730b4b6a74ba35afe0fa1d5ffca/examples/twisted/websocket/echo

好的,我使用Autobahn让它工作了。出于某种原因,我认为LineReceiver协议会是WebSocket,我仍在理解所有这些。非常感谢! - John Sardinha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接