52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
import socketio
|
|
import time
|
|
import json
|
|
|
|
# Create a Socket.IO client
|
|
sio = socketio.Client(logger=True, engineio_logger=True)
|
|
|
|
# Define event handlers from the server
|
|
@sio.event
|
|
def connect():
|
|
print("Connection established successfully!")
|
|
|
|
@sio.event
|
|
def disconnect():
|
|
print("Disconnected from server.")
|
|
|
|
@sio.event
|
|
def response(data):
|
|
print("\n[SERVER RESPONSE]:", data)
|
|
|
|
# This handler will catch all events that are not explicitly defined
|
|
@sio.on('*')
|
|
def catch_all(event, data):
|
|
"""
|
|
This function catches ALL events sent by the server.
|
|
"""
|
|
print("\n============== NEW EVENT ===============")
|
|
print(f"EVENT: '{event}'")
|
|
print("DATA:")
|
|
# Print data in a readable format
|
|
print(json.dumps(data, indent=2, ensure_ascii=False))
|
|
print("============================================")
|
|
|
|
# Main loop
|
|
if __name__ == '__main__':
|
|
try:
|
|
# Change the URL if your server runs on a different address
|
|
server_url = 'ws://localhost:8000'
|
|
print(f"Attempting to connect to {server_url}...")
|
|
# The path needs to match the server's Socket.IO path
|
|
sio.connect(server_url, socketio_path='socket.io')
|
|
|
|
print("\nListening for events from the server... (Press Ctrl+C to exit)")
|
|
sio.wait()
|
|
|
|
except socketio.exceptions.ConnectionError as e:
|
|
print(f"Connection failed: {e}")
|
|
except KeyboardInterrupt:
|
|
print("\nDisconnecting...")
|
|
sio.disconnect()
|
|
print("Exited.")
|