feat: improve websocket connection performance

This commit is contained in:
2021-07-05 12:04:00 -04:00
parent 4be068dae1
commit f8166435d1
2 changed files with 19 additions and 30 deletions

View File

@@ -6,7 +6,7 @@ from tetris.util import TextGenerator
from tetris.entity import PieceGenerator from tetris.entity import PieceGenerator
from tetris.entity import Well from tetris.entity import Well
from tetris.entity import Stack 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? # TODO should be a singleton and refactor the whole file?
class Game: class Game:
@@ -24,7 +24,7 @@ class Game:
def initialize(self) -> None: def initialize(self) -> None:
pygame.init() pygame.init()
OnlineService.init() MultiplayerService.init()
TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20)) TextGenerator.load(ConfigurationManager.get("image", "font"), (20, 20))
win_width = ConfigurationManager.get("window", "width") win_width = ConfigurationManager.get("window", "width")
@@ -52,8 +52,6 @@ class Game:
# gets called from the games main loop # gets called from the games main loop
def update(self) -> None: def update(self) -> None:
# TODO write not initialized exception # TODO write not initialized exception
OnlineService.update()
elapsed_time = self.clock.tick(self.fps) elapsed_time = self.clock.tick(self.fps)
if not self.next_piece: if not self.next_piece:
@@ -69,8 +67,6 @@ class Game:
pygame.quit() pygame.quit()
sys.exit() sys.exit()
OnlineService.ping_server()
if self.stack: if self.stack:
self.stack.update(elapsed_time, self) self.stack.update(elapsed_time, self)

View File

@@ -2,38 +2,31 @@ import asyncio
import websockets import websockets
from threading import Thread from threading import Thread
class OnlineService(): class MultiplayerService():
_URI = "ws://localhost:5001" # TODO remove hardcoded URI and add to config file
_TIMEOUT = 5 / 1000 # Timeout in seconds
_websocket = None
_thread = None _thread = None
@classmethod @classmethod
def init(cls): def init(cls):
thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(cls._connect_to_server(),)) thread = Thread(target=asyncio.get_event_loop().run_until_complete, args=(_NetworkConnectionService.init(),))
cls._thread_pool.append(thread)
thread.start() thread.start()
class _NetworkConnectionService():
_websocket = None
_URI = "ws://localhost:5001"
@classmethod @classmethod
def update(cls): async def init(cls):
cls._thread_pool = list(filter(lambda t: t.is_alive(), cls._thread_pool)) await cls._connect_to_server()
while True:
await asyncio.sleep(16e-3)
await cls._websocket.send("ping")
print(await cls._websocket.recv())
@classmethod @classmethod
async def _connect_to_server(cls): async def _connect_to_server(cls):
print("Connecting to server...") print("Connecting to server...")
cls._websocket = await websockets.connect(cls._URI) cls._websocket = await websockets.connect(cls._URI, ping_interval=None)
print("Connected to server...") # ping_interval=None is important, otherwise the server will disconnect us
# https://stackoverflow.com/a/58993145/11512104
@classmethod print("Connected to server...")
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