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

问题描述 投票:3回答:1

新的扭曲和实验。我正在尝试使用twisted.application和protocols.basic.LineReceiver为消息系统设置一个简单的websocket。

问题:Twisted应用程序连接客户端,并在(?)之后立即断开连接

尝试连接时客户端日志:

您的浏览器支持WebSocket!

Firefox无法在ws://127.0.0.1:1025 /上建立与服务器的连接。

连接已关闭......

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

2019-02-24T17:49:24 + 0000 [stdout #info]有了新客户!

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

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

2019-02-24T17: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-02-24T17:49:24 + 0000 [stdout #info]收到b'Accept:/'

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

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

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

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

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

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

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

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

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

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

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

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

2019-02-24T17: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和M​​acOs。有谁知道我做错了什么不能保持连接活着?

python websocket twisted
1个回答
1
投票

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

您正在运行WebSocket客户端。它打开TCP连接并开始向服务器说出WebSocket协议(从基于HTTP的握手开始)。它期望WebSocket协议响应此握手。

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

看看像https://crossbar.io/autobahn/这样适合使用WebSockets的库。您甚至可以在文档https://github.com/crossbario/autobahn-python/tree/9d65b508cc108730b4b6a74ba35afe0fa1d5ffca/examples/twisted/websocket/echo中找到示例WebSocket echo服务器

© www.soinside.com 2019 - 2024. All rights reserved.