Flushed out the server structure

This commit is contained in:
nolan 2025-09-22 22:29:34 -04:00
parent 6ae31fc186
commit 1232ebeb74
2 changed files with 33 additions and 12 deletions

View File

@ -1,7 +1,7 @@
from websockets import *
import asyncio, json
async def msg():
async def msg(): #TODO: Get rid of this boring boilerplate code
uri: str = "ws://127.0.0.1:8765"
async with connect(uri) as websocket:
send: str = input()

View File

@ -1,16 +1,37 @@
from websockets import *
import asyncio, json
import asyncio, json, signal, sys
async def test(websocket: ServerConnection) -> None:
message: str = await websocket.recv()
print(f"Server received: {message}")
response: str = f"Hello {websocket.id}. You sent {message}"
await websocket.send(response)
print(f"Server replied: {response}")
class WebSocketServer:
server: Server = None
async def main() -> None:
async with serve(test, "", 8765):
await asyncio.Future()
def __init__(self) -> None:
asyncio.run(self.main())
async def main(self) -> None:
async with serve(self.handleConnection, "", 8765) as sv:
self.server = sv
loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
loop.add_signal_handler(signal.SIGTERM, self.closeServer)
loop.add_reader(0, self.serverController)
await self.server.wait_closed()
async def handleConnection(self, connection: ServerConnection) -> None: #TODO: Make this actually do something
print(connection.remote_address)
message: str = await connection.recv()
print(f"Server received: {message}")
response: str = f"Hello {connection.id}. You sent {message}"
await connection.send(response)
print(f"Server replied: {response}")
def serverController(self) -> None: #TODO: Make ways of actually controlling the server with this
print("Server controller. Now die")
print(sys.stdin.readline())
self.closeServer()
def closeServer(self) -> None:
print("Closing down")
self.server.close()
if __name__ == "__main__":
asyncio.run(main())
WebSocketServer()
print("see ya suckers")