-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b79f82
commit eda5580
Showing
2 changed files
with
49 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import threading | ||
import json | ||
|
||
# websocket multi client support for using with queued information. | ||
# our client set which contains all connected websocket clients | ||
events_client_list = set() | ||
fft_client_list = set() | ||
states_client_list = set() | ||
|
||
def handle_connection(sock, client_list, event_queue): | ||
event_queue.put(json.dumps({"freedata-message": "hello-client"})) | ||
|
||
client_list.add(sock) | ||
while True: | ||
try: | ||
sock.receive(timeout=1) | ||
except Exception as e: | ||
print(f"client connection lost: {e}") | ||
try: | ||
client_list.remove(sock) | ||
except Exception as err: | ||
print(f"error removing client from list: {e} | {err}") | ||
break | ||
return | ||
|
||
def transmit_sock_data_worker(client_list, event_queue): | ||
while True: | ||
event = event_queue.get() | ||
clients = client_list.copy() | ||
for client in clients: | ||
try: | ||
client.send(event) | ||
except Exception: | ||
client_list.remove(client) | ||
|
||
# start a worker thread for every socket endpoint | ||
def startThreads(events_worker, d): | ||
events_thread = threading.Thread(target=transmit_sock_data_worker, daemon=True, args=(events_client_list, app.modem_events)) | ||
events_thread.start() | ||
|
||
states_thread = threading.Thread(target=transmit_sock_data_worker, daemon=True, args=(states_client_list, app.state_queue)) | ||
states_thread.start() | ||
|
||
fft_thread = threading.Thread(target=transmit_sock_data_worker, daemon=True, args=(fft_client_list, app.modem_fft)) | ||
fft_thread.start() |