aboutsummaryrefslogtreecommitdiff
path: root/examples.md
diff options
context:
space:
mode:
Diffstat (limited to 'examples.md')
-rw-r--r--examples.md156
1 files changed, 156 insertions, 0 deletions
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)
+```