generated from usnistgov/opensource-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Alex
committed
Nov 22, 2024
1 parent
30f4928
commit 907623f
Showing
4 changed files
with
77 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: Current File", | ||
"type": "python", | ||
"request": "launch", | ||
"program": "/Users/alexloembe/NIST/usnistgov/oar-pdr-py/python/tests/nistoar/midas/dbio/test_inmem.py", | ||
"console": "integratedTerminal", | ||
"pythonPath": "/Users/alexloembe/anaconda3/envs/dbio/bin/python", | ||
"env": { | ||
"PYTHONPATH": "/Users/alexloembe/NIST/usnistgov/oar-pdr-py/python", | ||
}, | ||
"terminal": "integrated", | ||
"justMyCode": false | ||
} | ||
] | ||
} | ||
|
Submodule metadata
updated
24 files
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,43 @@ | ||
# websocket_server.py | ||
import asyncio | ||
import websockets | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
class WebSocketServer: | ||
def __init__(self, host="localhost", port=8765): | ||
self.host = host | ||
self.port = port | ||
self.server = None | ||
self.clients = set() # Initialize the clients set | ||
|
||
async def start(self): | ||
self.server = await websockets.serve(self.websocket_handler, self.host, self.port) | ||
print(f"WebSocket server started on ws://{self.host}:{self.port}") | ||
|
||
|
||
async def websocket_handler(self, websocket, path): | ||
# Add the new client to the set | ||
self.clients.add(websocket) | ||
try: | ||
async for message in websocket: | ||
await self.send_message_to_clients(message) | ||
finally: | ||
# Remove the client from the set when they disconnect | ||
self.clients.remove(websocket) | ||
|
||
async def send_message_to_clients(self, message): | ||
if self.clients: | ||
await asyncio.wait([client.send(message) for client in self.clients]) | ||
|
||
def is_running(self): | ||
return self.server is not None and self.server.is_serving() | ||
|
||
def start_in_thread(self): | ||
executor = ThreadPoolExecutor(max_workers=1) | ||
executor.submit(self._start_event_loop) | ||
|
||
def _start_event_loop(self): | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
loop.run_until_complete(self.start()) | ||
|