the global IP of the host is now shown in the host lobby

This commit is contained in:
Faraphel 2023-02-27 15:53:11 +01:00
parent e961f7e348
commit 8b296f6965
2 changed files with 29 additions and 11 deletions

View file

@ -1,2 +1,3 @@
pyglet==2.0.4
numpy==1.24.2
requests

View file

@ -6,6 +6,7 @@ import requests
from source import network
from source.gui.scene.abc import Scene
from source.gui import widget, texture
from source.utils.thread import in_pyglet_context, StoppableThread
if TYPE_CHECKING:
from source.gui.window import Window
@ -16,13 +17,8 @@ class RoomHost(Scene):
def __init__(self, window: "Window", settings: "PacketSettings", **kwargs):
super().__init__(window, **kwargs)
"""r = requests.get('https://api.ipify.org')
r.raise_for_status()
ip_address: str = r.content.decode('utf8')
port: int = 52321"""
ip_address = "127.0.0.1"
port = 52321
self.ip_address: str = "127.0.0.1"
self.port: int = 52321
self.batch_button_background = pyglet.graphics.Batch()
self.batch_label = pyglet.graphics.Batch()
@ -47,7 +43,6 @@ class RoomHost(Scene):
x=0.5, y=0.55,
anchor_x="center", anchor_y="center",
text=f"Votre IP - {ip_address}:{port}",
font_size=20,
batch=self.batch_label
@ -64,11 +59,33 @@ class RoomHost(Scene):
batch=self.batch_label
)
self.thread = network.Host(window=self.window, daemon=True, settings=settings)
self.thread.start()
self.thread_network = network.Host(window=self.window, daemon=True, settings=settings)
self.thread_network.start()
self._refresh_ip_text()
self.thread_ip = StoppableThread(target=self._refresh_ip) # NOQA
self.thread_ip.start()
def _refresh_ip(self):
while True:
try:
response = requests.get('https://api.ipify.org')
response.raise_for_status()
break
except requests.HTTPError:
if self.thread_ip.stopped: return
self.ip_address: str = response.content.decode('utf8')
in_pyglet_context(self._refresh_ip_text)
def _refresh_ip_text(self):
self.label_ip.text = f"{self.ip_address}:{self.port}"
def button_back_callback(self, *_):
self.thread.stop()
self.thread_network.stop()
self.thread_ip.stop()
from source.gui.scene import MainMenu
self.window.set_scene(MainMenu)