-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketio_instance.py
More file actions
39 lines (29 loc) · 1004 Bytes
/
socketio_instance.py
File metadata and controls
39 lines (29 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Helper module to hold the server Socket.IO instance.
Other modules should call set_socketio(socketio_instance) during startup and
use get_socketio() or emit_event(...) to emit events safely.
"""
_socketio = None
def set_socketio(sio):
"""Store the Socket.IO server instance.
Args:
sio: instance returned by flask_socketio.SocketIO(...)
"""
global _socketio
_socketio = sio
def get_socketio():
"""Return the stored Socket.IO instance or None if not set."""
return _socketio
def emit_event(event, *args, **kwargs):
"""Emit an event using the stored Socket.IO instance if available.
This helper swallows exceptions to avoid breaking game logic when Socket.IO
is not available (e.g., in unit tests).
"""
sio = get_socketio()
if not sio:
return False
try:
sio.emit(event, *args, **kwargs)
return True
except Exception:
# swallow to keep server logic robust when socket fails
return False