-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
running websocket server and testclient
framework given for future message handling Co-authored-by: Bengt Wegner <[email protected]>
- Loading branch information
Showing
4 changed files
with
64 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
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,3 @@ | ||
class Statistics: | ||
def __init__(self) -> None: | ||
pass |
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,37 @@ | ||
from game import Game | ||
import asyncio | ||
import websockets | ||
import logging | ||
import json | ||
|
||
logger = logging.getLogger() | ||
|
||
async def handler(websocket): | ||
while True: | ||
try: | ||
message = await websocket.recv() | ||
except websockets.ConnectionClosedOK: | ||
logger.info("Connection Closed from Client-Side") | ||
# TODO: Add handling when game is not over yet | ||
break | ||
|
||
# TODO: Catch other errors for disconnects | ||
|
||
logger.info(f"Received: {message}") | ||
|
||
message_json = json.loads(message) | ||
match message_json["message_type"]: | ||
case "join": | ||
pass | ||
case _: | ||
await websocket.send("Invalid Message Type") | ||
|
||
|
||
async def main(): | ||
async with websockets.serve(handler, "", 8001): | ||
await asyncio.Future() # run forever | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) | ||
|
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 @@ | ||
#!/usr/bin/env python | ||
|
||
import asyncio | ||
from websockets.sync.client import connect | ||
import json | ||
|
||
def hello(): | ||
with connect("ws://localhost:8001") as websocket: | ||
|
||
obj = { | ||
"message_type": "asdf", | ||
"username": "my_username" | ||
} | ||
|
||
websocket.send(json.dumps(obj)) | ||
message = websocket.recv() | ||
print(f"Received: {message}") | ||
|
||
hello() |