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

Client connects and disconnects instantly with no error message?

twisted 和实验新手。我正在尝试使用 twisted.application 和 protocols.basic.LineReceiver.

为消息系统设置一个简单的 websocket

问题: Twisted 应用程序连接客户端,并在 (?)

后立即断开连接

尝试连接时的客户端日志:

WebSocket is supported by your Browser!

Firefox can’t establish a connection to the server at ws://127.0.0.1:1025/.

Connection is closed...

客户端尝试连接时服务器记录:

2019-02-24T17:49:24+0000 [stdout#info] Got new client!

2019-02-24T17:49:24+0000 [stdout#info] received b'GET / HTTP/1.1'

2019-02-24T17:49:24+0000 [stdout#info] received b'Host: 127.0.0.1:1025'

2019-02-24T17:49:24+0000 [stdout#info] received b'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:67.0) Gecko/20100101 Firefox/67.0'

2019-02-24T17:49:24+0000 [stdout#info] received b'Accept: /'

2019-02-24T17:49:24+0000 [stdout#info] received b'Accept-Language: en-US,en;q=0.5'

2019-02-24T17:49:24+0000 [stdout#info] received b'Accept-Encoding: gzip, deflate'

2019-02-24T17:49:24+0000 [stdout#info] received b'Sec-WebSocket-Version: 13'

2019-02-24T17:49:24+0000 [stdout#info] received b'Origin: null'

2019-02-24T17:49:24+0000 [stdout#info] received b'Sec-WebSocket-Extensions: permessage-deflate'

2019-02-24T17:49:24+0000 [stdout#info] received b'Sec-WebSocket-Key: /gN0KPBQZTU498eQBdTV2Q=='

2019-02-24T17:49:24+0000 [stdout#info] received b'DNT: 1'

2019-02-24T17:49:24+0000 [stdout#info] received b'Connection: keep-alive, Upgrade'

2019-02-24T17:49:24+0000 [stdout#info] received b'Pragma: no-cache'

2019-02-24T17:49:24+0000 [stdout#info] received b'Cache-Control: no-cache'

2019-02-24T17:49:24+0000 [stdout#info] received b'Upgrade: websocket'

2019-02-24T17:49:24+0000 [stdout#info] received b''

2019-02-24T17:49:24+0000 [stdout#info] Lost a client!

服务器代码:

"""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 和 MacO。有谁知道我做错了什么无法保持连接?

您是 运行 普通的 TCP 回显服务器。它接受 TCP 连接并回显他们发送的任何内容。

您是 运行 WebSocket 客户端。它打开一个 TCP 连接并开始与服务器对话 WebSocket 协议(从基于 HTTP 的握手开始)。它期望 WebSocket 协议对此握手做出响应。

TCP回显服务器将客户端的WebSocket握手数据发回给它。这不是正确的 WebSocket 握手响应。 WebSocket 客户端断定(正确地)服务器不是 WebSocket 服务器并断开连接。

看看类似 https://crossbar.io/autobahn/ for libraries geared towards working with WebSockets. You can even find an example WebSocket echo server in the documentation, https://github.com/crossbario/autobahn-python/tree/9d65b508cc108730b4b6a74ba35afe0fa1d5ffca/examples/twisted/websocket/echo

的内容