Flushed out the server structure
This commit is contained in:
parent
6ae31fc186
commit
1232ebeb74
@ -1,7 +1,7 @@
|
|||||||
from websockets import *
|
from websockets import *
|
||||||
import asyncio, json
|
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"
|
uri: str = "ws://127.0.0.1:8765"
|
||||||
async with connect(uri) as websocket:
|
async with connect(uri) as websocket:
|
||||||
send: str = input()
|
send: str = input()
|
||||||
|
43
server.py
43
server.py
@ -1,16 +1,37 @@
|
|||||||
from websockets import *
|
from websockets import *
|
||||||
import asyncio, json
|
import asyncio, json, signal, sys
|
||||||
|
|
||||||
async def test(websocket: ServerConnection) -> None:
|
class WebSocketServer:
|
||||||
message: str = await websocket.recv()
|
server: Server = None
|
||||||
print(f"Server received: {message}")
|
|
||||||
response: str = f"Hello {websocket.id}. You sent {message}"
|
|
||||||
await websocket.send(response)
|
|
||||||
print(f"Server replied: {response}")
|
|
||||||
|
|
||||||
async def main() -> None:
|
def __init__(self) -> None:
|
||||||
async with serve(test, "", 8765):
|
asyncio.run(self.main())
|
||||||
await asyncio.Future()
|
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
WebSocketServer()
|
||||||
|
print("see ya suckers")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user