全新的扭曲和实验。我正在尝试使用twisted.application和protocols.basic.LineReceiver为消息传递系统设置一个简单的websocket。
问题:扭曲的应用程序连接客户端,并在(?)
客户端尝试连接时记录:
您的浏览器支持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‘主机: 127.0.0.1:1025’ 2019-02-24T17:49:24+0000 stdout#info接收b‘’User Agent: Mozilla/5.0 (Macintosh;Intel 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语言: 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-版本: 13‘ 2019-02-24T17:49:24+0000 stdout#info收到b‘原产地: null’ 2019-02-24T17:49:24+0000 stdout#info收到b‘’Sec WebSocket-扩展: 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‘连接:保持-生存,升级’ 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‘升级: 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和MacOs。有人知道我做错了什么吗?我不能保持一种联系?
发布于 2019-02-25 15:49:50
您正在运行一个普通的TCP回送服务器。它接受TCP连接,不管它们发送什么,都会回显它们。
您正在运行一个WebSocket客户端。它打开一个TCP连接,并开始向服务器讲述WebSocket协议(从基于HTTP的握手开始)。它期望对此握手有WebSocket协议响应。
TCP服务器将客户端的WebSocket握手数据发回给它。这不是正确的WebSocket握手响应。WebSocket客户端(正确地)得出结论:服务器不是WebSocket服务器,并且断开连接。
请看一些类似于https://crossbar.io/autobahn/的东西,用于面向使用WebSockets的库。您甚至可以在文档https://github.com/crossbario/autobahn-python/tree/9d65b508cc108730b4b6a74ba35afe0fa1d5ffca/examples/twisted/websocket/echo中找到一个示例https://github.com/crossbario/autobahn-python/tree/9d65b508cc108730b4b6a74ba35afe0fa1d5ffca/examples/twisted/websocket/echo回送服务器。
https://stackoverflow.com/questions/54854839
复制相似问题