aboutsummaryrefslogtreecommitdiff
path: root/examples.md
blob: 58d947ff7149e434cab93a9be871c6253f566ea8 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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)
```