WebSock
WebSock copied to clipboard
Some tokens in client handshake data can be empty strings.
I use flutter and some tokens that come from flutter IOWebSocketChannel.connect are empty string.
def _opening_handshake(self, client, data):
...
for token in tokens[1:]:
label, value = token.split(": ", 1)
...
this code causes an exception. a token can be empty.
I will fix that. But does flutter send the empty token after it established the connection or while it is trying to connect?
I tried to use Websock with flutter but even could not send and receive a message. After fixing uppercase header fields in Websock sources it anyway reported about closed connection. It seems indigestible bugs in flutter. I used such Websock
from websock import WebSocketServer
from manager import *
import traceback
callback = None
def on_data_receive(client, rawdata):
'''Called by the WebSocket server when data is received.'''
global callback
try:
if not rawdata:
print('empty rawdata!')
return
data = json.loads(rawdata)
if callback:
if (data[0] == 'root' and data[1] is None):
GuiServer.callback = None
return
else:
result = callback(data[1]) #data[1] == returned value
GuiServer.callback = None
if result is None:
result = process(data) # rise up
else:
result = process(data)
if result:
if type(result) == Dialog:
callback = result.callback
ws.send(client, jsonString(prepareResult(result)))
except Exception as e:
print(e,traceback.format_exc())
def on_connection_open(client):
'''Called by the WebSocket server when a new connection is opened.'''
ws.send(client,jsonString([menu,get_screen()]))
def on_error(exception):
"""Called when the server returns an error
"""
print(f'on_error: {exception}')
def on_connection_close(client):
"""Called by the WebSocketServer when a connection is closed."""
#ws.send(client, "Closing socket")
print(client, 'closed')
def on_server_destruct():
"""Called immediately prior to the WebSocketServer shutting down."""
pass
ws = WebSocketServer("127.0.0.1", 1234,
on_data_receive=on_data_receive,
on_connection_open=on_connection_open,
on_error=on_error,
on_connection_close=on_connection_close)
ws.serve_forever()