feat: improve websocket connection performance
This commit is contained in:
@@ -6,7 +6,7 @@ from tetris.util import TextGenerator
|
||||
from tetris.entity import PieceGenerator
|
||||
from tetris.entity import Well
|
||||
from tetris.entity import Stack
|
||||
from tetris.online import OnlineService
|
||||
from tetris.online import MultiplayerService
|
||||
|
||||
# TODO should be a singleton and refactor the whole file?
|
||||
class Game:
|
||||
@@ -24,7 +24,7 @@ class Game:
|
||||
|
||||
def initialize(self) -> None:
|
||||
pygame.init()
|
||||
OnlineService.init()
|
||||
MultiplayerService.init()
|
||||
TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20))
|
||||
|
||||
win_width = ConfigurationManager.get("window", "width")
|
||||
@@ -52,8 +52,6 @@ class Game:
|
||||
# gets called from the games main loop
|
||||
def update(self) -> None:
|
||||
# TODO write not initialized exception
|
||||
OnlineService.update()
|
||||
|
||||
elapsed_time = self.clock.tick(self.fps)
|
||||
|
||||
if not self.next_piece:
|
||||
@@ -69,8 +67,6 @@ class Game:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
OnlineService.ping_server()
|
||||
|
||||
if self.stack:
|
||||
self.stack.update(elapsed_time, self)
|
||||
|
||||
|
||||
@@ -2,38 +2,31 @@ import asyncio
|
||||
import websockets
|
||||
from threading import Thread
|
||||
|
||||
class OnlineService():
|
||||
_URI = "ws://localhost:5001" # TODO remove hardcoded URI and add to config file
|
||||
_TIMEOUT = 5 / 1000 # Timeout in seconds
|
||||
_websocket = None
|
||||
class MultiplayerService():
|
||||
_thread = None
|
||||
|
||||
@classmethod
|
||||
def init(cls):
|
||||
thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(cls._connect_to_server(),))
|
||||
cls._thread_pool.append(thread)
|
||||
thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(_NetworkConnectionService.init(),))
|
||||
thread.start()
|
||||
|
||||
class _NetworkConnectionService():
|
||||
_websocket = None
|
||||
_URI = "ws://localhost:5001"
|
||||
|
||||
@classmethod
|
||||
def update(cls):
|
||||
cls._thread_pool = list(filter(lambda t: t.is_alive(), cls._thread_pool))
|
||||
async def init(cls):
|
||||
await cls._connect_to_server()
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(16e-3)
|
||||
await cls._websocket.send("ping")
|
||||
print(await cls._websocket.recv())
|
||||
|
||||
@classmethod
|
||||
async def _connect_to_server(cls):
|
||||
print("Connecting to server...")
|
||||
cls._websocket = await websockets.connect(cls._URI)
|
||||
print("Connected to server...")
|
||||
|
||||
@classmethod
|
||||
def ping_server(cls):
|
||||
thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(cls._ping_server(),))
|
||||
cls._thread_pool.append(thread)
|
||||
thread.start()
|
||||
|
||||
@classmethod
|
||||
async def _ping_server(cls):
|
||||
if cls._websocket is not None:
|
||||
await cls._websocket.send("ping")
|
||||
|
||||
# IDEAS
|
||||
# Create running ONE thread that handles requests and reponses to the server in one running loop
|
||||
cls._websocket = await websockets.connect(cls._URI, ping_interval=None)
|
||||
# ping_interval=None is important, otherwise the server will disconnect us
|
||||
# https://stackoverflow.com/a/58993145/11512104
|
||||
print("Connected to server...")
|
||||
Reference in New Issue
Block a user