New to twisted and experimenting. I'm trying to setup a simple websocket for a messaging system, using a twisted.application and a protocols.basic.LineReceiver.
Problem: Twisted application connects the client, and disconnects it right after (?)
Client logs when trying to connect:
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...
Server logs when client tries to connect:
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!
Server code:
"""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)
Running it with twistd -y chatserver.py
Simple client code: (This code worked well connecting to a pywebsocket ran locally)
<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>
I'm running Twisted 18.9.0, python 3.7.1 and on MacOs. Does anyone know what I'm doing wrong to not be able to keep a connection alive?
You're running a plain TCP echo server. It accepts TCP connections and echoes back to them whatever they send.
You're running a WebSocket client. It opens a TCP connection and begins to speak the WebSocket protocol (starting with an HTTP-based handshake) to the server. It expects a WebSocket protocol response to this handshake.
The TCP echo server sends the client's WebSocket handshake data back to it. This is not a correct WebSocket handshake response. The WebSocket client concludes (correctly) the server is not a WebSocket server and disconnects.
Take a look at something like 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