supress connection errors errors
This commit is contained in:
parent
45298bd360
commit
5beb79b51b
@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import asyncio
|
||||
import websockets
|
||||
from websockets.server import serve
|
||||
from datetime import datetime
|
||||
import json
|
||||
@ -20,7 +21,7 @@ async def sendToClient(client, msg):
|
||||
if client['state'] != 'terminated':
|
||||
try:
|
||||
await client['ws'].send(msg)
|
||||
except ConnectionClosed:
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
if client['state'] != 'kicked':
|
||||
client['state'] = 'terminated'
|
||||
if client['boat'] is not None:
|
||||
@ -207,7 +208,6 @@ async def new_boat(boatId, name, ws):
|
||||
|
||||
async def run(ws, path):
|
||||
"""hadeler for every new websocket connection"""
|
||||
print("new websocket connection: " + path)
|
||||
client = None
|
||||
async for msg in ws:
|
||||
for message in msg.split("\n"):
|
||||
@ -217,28 +217,37 @@ async def run(ws, path):
|
||||
if len(message) == 3 and message[1] == "4675":
|
||||
client = await new_client(message[0], ws)
|
||||
if client is not None:
|
||||
async for msg in ws:
|
||||
for message in msg.split("\n"):
|
||||
if len(message) == 0:
|
||||
continue
|
||||
if client['state'] == 'active':
|
||||
await on_message(message, client)
|
||||
else:
|
||||
break
|
||||
|
||||
try:
|
||||
async for msg in ws:
|
||||
for message in msg.split("\n"):
|
||||
if len(message) == 0:
|
||||
continue
|
||||
if client['state'] == 'active':
|
||||
await on_message(message, client)
|
||||
else:
|
||||
break
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
print("INFO: client " + client['id'] + ": disconnected")
|
||||
client['state'] = "terminated"
|
||||
break
|
||||
elif (len(message) == 3 and message[1] == "3440"):
|
||||
client = await new_boat(message[0], message[2], ws)
|
||||
if client is not None:
|
||||
async for msg in ws:
|
||||
for message in msg.split("\n"):
|
||||
if len(message) == 0:
|
||||
continue
|
||||
print("boat" + client['id'] + " says '" + message + "'")
|
||||
client['log'].append({
|
||||
"t": datetime.timestamp(datetime.now()),
|
||||
"type": "rx",
|
||||
"msg": message
|
||||
})
|
||||
try:
|
||||
async for msg in ws:
|
||||
for message in msg.split("\n"):
|
||||
if len(message) == 0:
|
||||
continue
|
||||
print("boat" + client['id'] + " says '" + message + "'")
|
||||
client['log'].append({
|
||||
"t": datetime.timestamp(datetime.now()),
|
||||
"type": "rx",
|
||||
"msg": message
|
||||
})
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
print("INFO: boat " + client['id'] + ": disconnected")
|
||||
client['state'] = "terminated"
|
||||
break
|
||||
|
||||
async def main():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user