新手尝试使用 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] 失去一个客户端! 服务器代码:
我正在使用 Twisted 18.9.0,Python 3.7.1,并在MacOS上运行。 有人知道我做错了什么导致无法保持连接吗?
问题: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上运行。 有人知道我做错了什么导致无法保持连接吗?