Skip to content

Commit

Permalink
running websocket server and testclient
Browse files Browse the repository at this point in the history
framework given for future message handling

Co-authored-by: Bengt Wegner <[email protected]>
  • Loading branch information
JM-Lemmi and Petzys committed Feb 22, 2024
1 parent a8a3b4e commit 674a81c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Server/player.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from statistics import Statistics

class Player:
"""
Represents a player in the game.
Expand All @@ -22,7 +24,7 @@ class RemotePlayer(Player):
statistics (Statistics): The statistics of the player.
"""
def __init__(self, id: int, display_name: str, statistics: Statistics):
super(id, display_name, statistics)
super().__init__(id, display_name, statistics)

class LocalPlayer(Player):
"""
Expand All @@ -34,7 +36,7 @@ class LocalPlayer(Player):
statistics (Statistics): The statistics of the player.
"""
def __init__(self, id: int, display_name: str, statistics: Statistics):
super(id, display_name, statistics)
super().__init__(id, display_name, statistics)

class ComputerPlayer(Player):
"""
Expand All @@ -46,4 +48,4 @@ class ComputerPlayer(Player):
statistics (Statistics): The statistics of the player.
"""
def __init__(self, id: int, display_name: str, statistics: Statistics):
super(id, display_name, statistics)
super().__init__(id, display_name, statistics)
3 changes: 3 additions & 0 deletions Server/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Statistics:
def __init__(self) -> None:
pass
37 changes: 37 additions & 0 deletions Server/websocket_server.py
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())

19 changes: 19 additions & 0 deletions Server/websocket_testclient.py
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()

0 comments on commit 674a81c

Please sign in to comment.