From 3a975db66a3711f34e8b64bb27a8eaca79fdeca9 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Sun, 1 Feb 2026 13:14:32 -0600 Subject: Initial commit --- examples.md | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 examples.md (limited to 'examples.md') diff --git a/examples.md b/examples.md new file mode 100644 index 0000000..58d947f --- /dev/null +++ b/examples.md @@ -0,0 +1,156 @@ +# Usage Examples + +## Simple Chat Application + +### Hub Side (Server) +```lua +Hub = require("hub").Hub +log = require("log") + +-- Create and initialize hub +hub = Hub() +hub:initialize() + +-- Get the hub ID and share it with clients +print("Hub ID:", hub:get_peer_id()) + +-- Listen for client events +hub:on_connect(function(client_id) + log.info("New client: " .. client_id, {"chat", "connection"}) +end) + +hub:on_disconnect(function(client_id) + log.info("Client left: " .. client_id, {"chat", "connection"}) +end) + +-- Main loop +function love.update(dt) + hub:pump() +end +``` + +### Client Side +```lua +Client = require("client").Client +log = require("log") + +-- Create client with a name +client = Client("Alice") + +-- Connect to hub (use the ID printed by hub) +client:connect_to_hub("hub-id-here") + +-- Register router for chat messages +client:register_router("chat", function(from_id, data) + print(data.username .. ": " .. data.message) +end) + +-- Handle connection +client:on_connect(function() + print("Connected to chat!") +end) + +-- Send a chat message to all +function send_chat(message) + client:broadcast("chat", { + username = "Alice", + message = message + }) +end + +-- Main loop +function love.update(dt) + client:pump() +end +``` + +## Game State Synchronization + +### Hub Side +```lua +Hub = require("hub").Hub + +hub = Hub() +hub:initialize() + +-- Track game state +game_state = { + players = {}, + started = false +} + +hub:on_connect(function(client_id) + -- Send current game state to new player + hub:send_to_client(client_id, { + type = "game_state", + from = "server", + data = game_state + }) +end) + +function love.update(dt) + hub:pump() +end +``` + +### Client Side +```lua +Client = require("client").Client + +client = Client("Player1") +client:connect_to_hub(hub_id) + +local_game_state = {} + +-- Handle game state updates +client:register_router("game_state", function(from_id, data) + local_game_state = data + print("Game state updated") +end) + +-- Handle player actions +client:register_router("player_move", function(from_id, data) + -- Update other player's position + update_player_position(from_id, data.x, data.y) +end) + +-- Send player movement +function move_player(x, y) + client:broadcast("player_move", { + x = x, + y = y + }) +end + +function love.update(dt) + client:pump() +end +``` + +## Logging Examples + +```lua +log = require("log") + +-- Basic logging +log.info("Server started", {"server"}) +log.error("Connection failed", {"network", "error"}) + +-- Query logs by level +log.of_level("error", function(msg) + print(string.format("[%s] %s", msg.level, msg.message)) +end) + +-- Query logs by tags +log.of_tags({"network"}, function(msg) + print("Network event:", msg.message) +end) + +-- Real-time monitoring +log.listen(function(msg) + if msg.level == "panic" then + -- Handle critical errors + crash_handler(msg.message) + end +end) +``` -- cgit v1.2.3-70-g09d2