@air-jam/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/chunk-XSXI3RIS.js +72 -0
- package/dist/chunk-XSXI3RIS.js.map +1 -0
- package/dist/index.cjs +3514 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1529 -0
- package/dist/index.d.ts +1529 -0
- package/dist/index.js +3366 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.cjs +85 -0
- package/dist/protocol.cjs.map +1 -0
- package/dist/protocol.d.cts +242 -0
- package/dist/protocol.d.ts +242 -0
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -0
- package/package.json +81 -0
- package/src/AirJamClient.ts +775 -0
- package/src/audio/audio-manager.ts +395 -0
- package/src/audio/hooks.tsx +140 -0
- package/src/audio/index.ts +3 -0
- package/src/audio/volume-store.ts +77 -0
- package/src/components/air-jam-overlay.tsx +456 -0
- package/src/components/controller-shell.tsx +329 -0
- package/src/components/qr-scanner-dialog.tsx +373 -0
- package/src/components/ui/alert.tsx +66 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +47 -0
- package/src/components/ui/button.tsx +61 -0
- package/src/components/ui/card.tsx +92 -0
- package/src/components/ui/slider.tsx +61 -0
- package/src/components/ui/tabs.tsx +64 -0
- package/src/components/volume-controls.tsx +93 -0
- package/src/constants.ts +45 -0
- package/src/context/air-jam-context.tsx +478 -0
- package/src/context/index.ts +2 -0
- package/src/context/socket-manager.ts +98 -0
- package/src/events.ts +151 -0
- package/src/hooks/use-air-jam-controller.ts +608 -0
- package/src/hooks/use-air-jam-host.ts +624 -0
- package/src/hooks/use-air-jam-shell.ts +343 -0
- package/src/hooks/use-fullscreen.ts +108 -0
- package/src/hooks/use-get-input.ts +97 -0
- package/src/hooks/use-send-signal.ts +164 -0
- package/src/index.ts +33 -0
- package/src/internal/input-manager.ts +362 -0
- package/src/protocol.ts +331 -0
- package/src/state/connection-store.ts +84 -0
- package/src/styles.css +128 -0
- package/src/types/browser.ts +42 -0
- package/src/utils/cn.ts +4 -0
- package/src/utils/ids.ts +31 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/mode.ts +13 -0
- package/src/utils/network-ip.ts +145 -0
- package/src/utils/url-builder.ts +170 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { io, Socket } from "socket.io-client";
|
|
2
|
+
import type { StoreApi } from "zustand";
|
|
3
|
+
import type {
|
|
4
|
+
ClientToServerEvents,
|
|
5
|
+
ConnectionRole,
|
|
6
|
+
ControllerInputPayload,
|
|
7
|
+
ControllerJoinedNotice,
|
|
8
|
+
ControllerLeftNotice,
|
|
9
|
+
GameState,
|
|
10
|
+
ServerToClientEvents,
|
|
11
|
+
} from "./protocol";
|
|
12
|
+
import { createAirJamStore, type AirJamStore } from "./state/connection-store";
|
|
13
|
+
import { detectRunMode } from "./utils/mode";
|
|
14
|
+
import { urlBuilder } from "./utils/url-builder";
|
|
15
|
+
|
|
16
|
+
export interface AirJamClientOptions {
|
|
17
|
+
apiKey?: string;
|
|
18
|
+
serverUrl?: string;
|
|
19
|
+
role: ConnectionRole;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The core Air Jam Engine.
|
|
24
|
+
* Manages the Socket.io connection and the state store for a single Air Jam session.
|
|
25
|
+
* This class is framework-agnostic and can be used outside of React.
|
|
26
|
+
*/
|
|
27
|
+
export class AirJamClient {
|
|
28
|
+
public readonly socket: Socket<ServerToClientEvents, ClientToServerEvents>;
|
|
29
|
+
public readonly store: StoreApi<AirJamStore>;
|
|
30
|
+
public readonly role: ConnectionRole;
|
|
31
|
+
private readonly _inputBuffer: Map<string, ControllerInputPayload> =
|
|
32
|
+
new Map();
|
|
33
|
+
private _listenerRegisteredAt?: number;
|
|
34
|
+
public readonly _clientInstanceId: string;
|
|
35
|
+
|
|
36
|
+
constructor(options: AirJamClientOptions) {
|
|
37
|
+
this.role = options.role;
|
|
38
|
+
this._clientInstanceId = `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
39
|
+
|
|
40
|
+
// 1. Initialize State
|
|
41
|
+
this.store = createAirJamStore();
|
|
42
|
+
this.store.getState().setRole(options.role);
|
|
43
|
+
this.store.getState().setMode(detectRunMode());
|
|
44
|
+
|
|
45
|
+
// 2. Resolve Server URL
|
|
46
|
+
const resolvedUrl = options.serverUrl || urlBuilder.resolveServerUrl();
|
|
47
|
+
|
|
48
|
+
// 3. Initialize Socket
|
|
49
|
+
this.socket = io(resolvedUrl, {
|
|
50
|
+
autoConnect: false,
|
|
51
|
+
reconnection: true,
|
|
52
|
+
transports: ["websocket"],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// 4. Set up core listeners
|
|
56
|
+
this.setupListeners();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private setupHostListeners() {
|
|
60
|
+
if (this.role !== "host") return;
|
|
61
|
+
|
|
62
|
+
// Remove existing listeners to avoid duplicates
|
|
63
|
+
this.socket.off("server:controllerJoined");
|
|
64
|
+
this.socket.off("server:controllerLeft");
|
|
65
|
+
this.socket.off("server:input");
|
|
66
|
+
|
|
67
|
+
// #region agent log
|
|
68
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: { "Content-Type": "application/json" },
|
|
71
|
+
body: JSON.stringify({
|
|
72
|
+
location: "AirJamClient.ts:56",
|
|
73
|
+
message: "setupHostListeners called - re-registering host listeners",
|
|
74
|
+
data: {
|
|
75
|
+
role: this.role,
|
|
76
|
+
socketId: this.socket.id,
|
|
77
|
+
socketConnected: this.socket.connected,
|
|
78
|
+
clientInstanceId: this._clientInstanceId,
|
|
79
|
+
},
|
|
80
|
+
timestamp: Date.now(),
|
|
81
|
+
sessionId: "debug-session",
|
|
82
|
+
runId: "run1",
|
|
83
|
+
hypothesisId: "HOST_LISTENERS_SETUP",
|
|
84
|
+
}),
|
|
85
|
+
}).catch(() => {});
|
|
86
|
+
// #endregion
|
|
87
|
+
|
|
88
|
+
this.socket.on(
|
|
89
|
+
"server:controllerJoined",
|
|
90
|
+
(payload: ControllerJoinedNotice) => {
|
|
91
|
+
// #region agent log
|
|
92
|
+
const logData = {
|
|
93
|
+
location: "AirJamClient.ts:62",
|
|
94
|
+
message: "CLIENT: server:controllerJoined event received",
|
|
95
|
+
data: {
|
|
96
|
+
role: this.role,
|
|
97
|
+
socketId: this.socket.id,
|
|
98
|
+
socketConnected: this.socket.connected,
|
|
99
|
+
payload: JSON.stringify(payload),
|
|
100
|
+
hasPlayer: !!payload.player,
|
|
101
|
+
clientInstanceId: this._clientInstanceId,
|
|
102
|
+
},
|
|
103
|
+
timestamp: Date.now(),
|
|
104
|
+
sessionId: "debug-session",
|
|
105
|
+
runId: "run1",
|
|
106
|
+
hypothesisId: "CLIENT_CONTROLLER_JOINED",
|
|
107
|
+
};
|
|
108
|
+
console.log(`[DEBUG] ${logData.message}`, logData.data);
|
|
109
|
+
fetch(
|
|
110
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
111
|
+
{
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: { "Content-Type": "application/json" },
|
|
114
|
+
body: JSON.stringify(logData),
|
|
115
|
+
},
|
|
116
|
+
).catch(() => {});
|
|
117
|
+
// #endregion
|
|
118
|
+
console.log(`[AirJamClient] server:controllerJoined`, payload);
|
|
119
|
+
if (payload.player) {
|
|
120
|
+
// #region agent log
|
|
121
|
+
fetch(
|
|
122
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
123
|
+
{
|
|
124
|
+
method: "POST",
|
|
125
|
+
headers: { "Content-Type": "application/json" },
|
|
126
|
+
body: JSON.stringify({
|
|
127
|
+
location: "AirJamClient.ts:70",
|
|
128
|
+
message: "CLIENT: calling store.upsertPlayer",
|
|
129
|
+
data: {
|
|
130
|
+
playerId: payload.player.id,
|
|
131
|
+
playerLabel: payload.player.label,
|
|
132
|
+
currentPlayersCount: this.store.getState().players.length,
|
|
133
|
+
clientInstanceId: this._clientInstanceId,
|
|
134
|
+
},
|
|
135
|
+
timestamp: Date.now(),
|
|
136
|
+
sessionId: "debug-session",
|
|
137
|
+
runId: "run1",
|
|
138
|
+
hypothesisId: "CLIENT_CONTROLLER_JOINED",
|
|
139
|
+
}),
|
|
140
|
+
},
|
|
141
|
+
).catch(() => {});
|
|
142
|
+
// #endregion
|
|
143
|
+
this.store.getState().upsertPlayer(payload.player);
|
|
144
|
+
// #region agent log
|
|
145
|
+
const newPlayersCount = this.store.getState().players.length;
|
|
146
|
+
fetch(
|
|
147
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
148
|
+
{
|
|
149
|
+
method: "POST",
|
|
150
|
+
headers: { "Content-Type": "application/json" },
|
|
151
|
+
body: JSON.stringify({
|
|
152
|
+
location: "AirJamClient.ts:75",
|
|
153
|
+
message: "CLIENT: store.upsertPlayer completed",
|
|
154
|
+
data: {
|
|
155
|
+
playerId: payload.player.id,
|
|
156
|
+
newPlayersCount,
|
|
157
|
+
clientInstanceId: this._clientInstanceId,
|
|
158
|
+
},
|
|
159
|
+
timestamp: Date.now(),
|
|
160
|
+
sessionId: "debug-session",
|
|
161
|
+
runId: "run1",
|
|
162
|
+
hypothesisId: "CLIENT_CONTROLLER_JOINED",
|
|
163
|
+
}),
|
|
164
|
+
},
|
|
165
|
+
).catch(() => {});
|
|
166
|
+
// #endregion
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
this.socket.on("server:controllerLeft", (payload: ControllerLeftNotice) => {
|
|
172
|
+
this.store.getState().removePlayer(payload.controllerId);
|
|
173
|
+
this._inputBuffer.delete(payload.controllerId);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
this.socket.on("server:input", (payload) => {
|
|
177
|
+
this._inputBuffer.set(payload.controllerId, payload.input);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private setupServerStateListener() {
|
|
182
|
+
// Remove existing listener to avoid duplicates
|
|
183
|
+
this.socket.off("server:state");
|
|
184
|
+
|
|
185
|
+
// #region agent log
|
|
186
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
187
|
+
method: "POST",
|
|
188
|
+
headers: { "Content-Type": "application/json" },
|
|
189
|
+
body: JSON.stringify({
|
|
190
|
+
location: "AirJamClient.ts:59",
|
|
191
|
+
message: "setupServerStateListener called",
|
|
192
|
+
data: {
|
|
193
|
+
role: this.role,
|
|
194
|
+
socketId: this.socket.id,
|
|
195
|
+
socketConnected: this.socket.connected,
|
|
196
|
+
clientInstanceId: this._clientInstanceId,
|
|
197
|
+
},
|
|
198
|
+
timestamp: Date.now(),
|
|
199
|
+
sessionId: "debug-session",
|
|
200
|
+
runId: "run1",
|
|
201
|
+
hypothesisId: "I",
|
|
202
|
+
}),
|
|
203
|
+
}).catch(() => {});
|
|
204
|
+
// #endregion
|
|
205
|
+
|
|
206
|
+
const serverStateHandler = (payload: {
|
|
207
|
+
state: { gameState?: GameState; message?: string };
|
|
208
|
+
}) => {
|
|
209
|
+
// #region agent log
|
|
210
|
+
fetch(
|
|
211
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
212
|
+
{
|
|
213
|
+
method: "POST",
|
|
214
|
+
headers: { "Content-Type": "application/json" },
|
|
215
|
+
body: JSON.stringify({
|
|
216
|
+
location: "AirJamClient.ts:64",
|
|
217
|
+
message: "CLIENT: server:state event received in handler",
|
|
218
|
+
data: {
|
|
219
|
+
role: this.role,
|
|
220
|
+
gameState: payload.state.gameState,
|
|
221
|
+
message: payload.state.message,
|
|
222
|
+
fullPayload: JSON.stringify(payload),
|
|
223
|
+
socketId: this.socket.id,
|
|
224
|
+
socketConnected: this.socket.connected,
|
|
225
|
+
listenerRegisteredAt: this._listenerRegisteredAt,
|
|
226
|
+
clientInstanceId: this._clientInstanceId,
|
|
227
|
+
},
|
|
228
|
+
timestamp: Date.now(),
|
|
229
|
+
sessionId: "debug-session",
|
|
230
|
+
runId: "run1",
|
|
231
|
+
hypothesisId: "CLIENT_RECEIVE",
|
|
232
|
+
}),
|
|
233
|
+
},
|
|
234
|
+
).catch(() => {});
|
|
235
|
+
// #endregion
|
|
236
|
+
const store = this.store.getState();
|
|
237
|
+
// #region agent log
|
|
238
|
+
fetch(
|
|
239
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
240
|
+
{
|
|
241
|
+
method: "POST",
|
|
242
|
+
headers: { "Content-Type": "application/json" },
|
|
243
|
+
body: JSON.stringify({
|
|
244
|
+
location: "AirJamClient.ts:75",
|
|
245
|
+
message: "checking gameState in payload",
|
|
246
|
+
data: {
|
|
247
|
+
hasGameState: !!payload.state.gameState,
|
|
248
|
+
gameStateValue: payload.state.gameState,
|
|
249
|
+
currentStoreState: store.gameState,
|
|
250
|
+
},
|
|
251
|
+
timestamp: Date.now(),
|
|
252
|
+
sessionId: "debug-session",
|
|
253
|
+
runId: "run1",
|
|
254
|
+
hypothesisId: "B",
|
|
255
|
+
}),
|
|
256
|
+
},
|
|
257
|
+
).catch(() => {});
|
|
258
|
+
// #endregion
|
|
259
|
+
if (payload.state.gameState) {
|
|
260
|
+
// #region agent log
|
|
261
|
+
fetch(
|
|
262
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
263
|
+
{
|
|
264
|
+
method: "POST",
|
|
265
|
+
headers: { "Content-Type": "application/json" },
|
|
266
|
+
body: JSON.stringify({
|
|
267
|
+
location: "AirJamClient.ts:77",
|
|
268
|
+
message: "calling store.setGameState",
|
|
269
|
+
data: {
|
|
270
|
+
newGameState: payload.state.gameState,
|
|
271
|
+
currentGameState: store.gameState,
|
|
272
|
+
},
|
|
273
|
+
timestamp: Date.now(),
|
|
274
|
+
sessionId: "debug-session",
|
|
275
|
+
runId: "run1",
|
|
276
|
+
hypothesisId: "B",
|
|
277
|
+
}),
|
|
278
|
+
},
|
|
279
|
+
).catch(() => {});
|
|
280
|
+
// #endregion
|
|
281
|
+
store.setGameState(payload.state.gameState);
|
|
282
|
+
}
|
|
283
|
+
if (payload.state.message !== undefined) {
|
|
284
|
+
store.setStateMessage(payload.state.message);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
// #region agent log
|
|
288
|
+
this._listenerRegisteredAt = Date.now();
|
|
289
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
290
|
+
method: "POST",
|
|
291
|
+
headers: { "Content-Type": "application/json" },
|
|
292
|
+
body: JSON.stringify({
|
|
293
|
+
location: "AirJamClient.ts:91",
|
|
294
|
+
message: "registering server:state listener",
|
|
295
|
+
data: {
|
|
296
|
+
role: this.role,
|
|
297
|
+
socketId: this.socket.id,
|
|
298
|
+
socketConnected: this.socket.connected,
|
|
299
|
+
clientInstanceId: this._clientInstanceId,
|
|
300
|
+
listenerRegisteredAt: this._listenerRegisteredAt,
|
|
301
|
+
},
|
|
302
|
+
timestamp: Date.now(),
|
|
303
|
+
sessionId: "debug-session",
|
|
304
|
+
runId: "run1",
|
|
305
|
+
hypothesisId: "H",
|
|
306
|
+
}),
|
|
307
|
+
}).catch(() => {});
|
|
308
|
+
// #endregion
|
|
309
|
+
// Wrap the handler to log when it's actually attached
|
|
310
|
+
const wrappedHandler = (payload: {
|
|
311
|
+
state: { gameState?: GameState; message?: string };
|
|
312
|
+
}) => {
|
|
313
|
+
// #region agent log
|
|
314
|
+
fetch(
|
|
315
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
316
|
+
{
|
|
317
|
+
method: "POST",
|
|
318
|
+
headers: { "Content-Type": "application/json" },
|
|
319
|
+
body: JSON.stringify({
|
|
320
|
+
location: "AirJamClient.ts:86",
|
|
321
|
+
message: "WRAPPED_HANDLER: server:state handler invoked",
|
|
322
|
+
data: {
|
|
323
|
+
role: this.role,
|
|
324
|
+
socketId: this.socket.id,
|
|
325
|
+
socketConnected: this.socket.connected,
|
|
326
|
+
clientInstanceId: this._clientInstanceId,
|
|
327
|
+
payload: JSON.stringify(payload),
|
|
328
|
+
},
|
|
329
|
+
timestamp: Date.now(),
|
|
330
|
+
sessionId: "debug-session",
|
|
331
|
+
runId: "run1",
|
|
332
|
+
hypothesisId: "WRAPPED",
|
|
333
|
+
}),
|
|
334
|
+
},
|
|
335
|
+
).catch(() => {});
|
|
336
|
+
// #endregion
|
|
337
|
+
serverStateHandler(payload);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
this.socket.on("server:state", wrappedHandler);
|
|
341
|
+
// #region agent log
|
|
342
|
+
const listenerCount = this.socket.listeners("server:state").length;
|
|
343
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
344
|
+
method: "POST",
|
|
345
|
+
headers: { "Content-Type": "application/json" },
|
|
346
|
+
body: JSON.stringify({
|
|
347
|
+
location: "AirJamClient.ts:95",
|
|
348
|
+
message: "server:state listener registered - checking socket listeners",
|
|
349
|
+
data: {
|
|
350
|
+
role: this.role,
|
|
351
|
+
socketId: this.socket.id,
|
|
352
|
+
socketConnected: this.socket.connected,
|
|
353
|
+
clientInstanceId: this._clientInstanceId,
|
|
354
|
+
hasListeners: listenerCount > 0,
|
|
355
|
+
listenerCount,
|
|
356
|
+
},
|
|
357
|
+
timestamp: Date.now(),
|
|
358
|
+
sessionId: "debug-session",
|
|
359
|
+
runId: "run1",
|
|
360
|
+
hypothesisId: "H",
|
|
361
|
+
}),
|
|
362
|
+
}).catch(() => {});
|
|
363
|
+
// #endregion
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private setupListeners() {
|
|
367
|
+
// #region agent log
|
|
368
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
369
|
+
method: "POST",
|
|
370
|
+
headers: { "Content-Type": "application/json" },
|
|
371
|
+
body: JSON.stringify({
|
|
372
|
+
location: "AirJamClient.ts:89",
|
|
373
|
+
message: "setupListeners called",
|
|
374
|
+
data: {
|
|
375
|
+
role: this.role,
|
|
376
|
+
socketId: this.socket.id,
|
|
377
|
+
socketConnected: this.socket.connected,
|
|
378
|
+
clientInstanceId: this._clientInstanceId,
|
|
379
|
+
},
|
|
380
|
+
timestamp: Date.now(),
|
|
381
|
+
sessionId: "debug-session",
|
|
382
|
+
runId: "run1",
|
|
383
|
+
hypothesisId: "SETUP",
|
|
384
|
+
}),
|
|
385
|
+
}).catch(() => {});
|
|
386
|
+
// #endregion
|
|
387
|
+
// Handle connect event
|
|
388
|
+
const onConnect = () => {
|
|
389
|
+
// #region agent log
|
|
390
|
+
fetch(
|
|
391
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
392
|
+
{
|
|
393
|
+
method: "POST",
|
|
394
|
+
headers: { "Content-Type": "application/json" },
|
|
395
|
+
body: JSON.stringify({
|
|
396
|
+
location: "AirJamClient.ts:92",
|
|
397
|
+
message: "socket connected - onConnect handler fired",
|
|
398
|
+
data: {
|
|
399
|
+
role: this.role,
|
|
400
|
+
socketId: this.socket.id,
|
|
401
|
+
clientInstanceId: this._clientInstanceId,
|
|
402
|
+
listenerRegisteredAt: this._listenerRegisteredAt,
|
|
403
|
+
},
|
|
404
|
+
timestamp: Date.now(),
|
|
405
|
+
sessionId: "debug-session",
|
|
406
|
+
runId: "run1",
|
|
407
|
+
hypothesisId: "G",
|
|
408
|
+
}),
|
|
409
|
+
},
|
|
410
|
+
).catch(() => {});
|
|
411
|
+
// #endregion
|
|
412
|
+
this.store.getState().setStatus("connected");
|
|
413
|
+
this.store.getState().setError(undefined);
|
|
414
|
+
// Re-register all listeners after connection to ensure they're active
|
|
415
|
+
// This fixes the issue where listeners registered before socket connects don't work
|
|
416
|
+
// #region agent log
|
|
417
|
+
fetch(
|
|
418
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
419
|
+
{
|
|
420
|
+
method: "POST",
|
|
421
|
+
headers: { "Content-Type": "application/json" },
|
|
422
|
+
body: JSON.stringify({
|
|
423
|
+
location: "AirJamClient.ts:98",
|
|
424
|
+
message: "about to re-register all listeners in onConnect",
|
|
425
|
+
data: {
|
|
426
|
+
role: this.role,
|
|
427
|
+
socketId: this.socket.id,
|
|
428
|
+
socketConnected: this.socket.connected,
|
|
429
|
+
clientInstanceId: this._clientInstanceId,
|
|
430
|
+
},
|
|
431
|
+
timestamp: Date.now(),
|
|
432
|
+
sessionId: "debug-session",
|
|
433
|
+
runId: "run1",
|
|
434
|
+
hypothesisId: "G",
|
|
435
|
+
}),
|
|
436
|
+
},
|
|
437
|
+
).catch(() => {});
|
|
438
|
+
// #endregion
|
|
439
|
+
this.setupServerStateListener();
|
|
440
|
+
this.setupHostListeners();
|
|
441
|
+
};
|
|
442
|
+
// #region agent log
|
|
443
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
444
|
+
method: "POST",
|
|
445
|
+
headers: { "Content-Type": "application/json" },
|
|
446
|
+
body: JSON.stringify({
|
|
447
|
+
location: "AirJamClient.ts:100",
|
|
448
|
+
message: "registering connect event listener",
|
|
449
|
+
data: {
|
|
450
|
+
role: this.role,
|
|
451
|
+
socketId: this.socket.id,
|
|
452
|
+
socketConnected: this.socket.connected,
|
|
453
|
+
clientInstanceId: this._clientInstanceId,
|
|
454
|
+
},
|
|
455
|
+
timestamp: Date.now(),
|
|
456
|
+
sessionId: "debug-session",
|
|
457
|
+
runId: "run1",
|
|
458
|
+
hypothesisId: "SETUP",
|
|
459
|
+
}),
|
|
460
|
+
}).catch(() => {});
|
|
461
|
+
// #endregion
|
|
462
|
+
this.socket.on("connect", onConnect);
|
|
463
|
+
|
|
464
|
+
// If socket is already connected, call handler immediately
|
|
465
|
+
if (this.socket.connected) {
|
|
466
|
+
// #region agent log
|
|
467
|
+
fetch(
|
|
468
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
469
|
+
{
|
|
470
|
+
method: "POST",
|
|
471
|
+
headers: { "Content-Type": "application/json" },
|
|
472
|
+
body: JSON.stringify({
|
|
473
|
+
location: "AirJamClient.ts:103",
|
|
474
|
+
message:
|
|
475
|
+
"socket already connected when listener registered - calling onConnect immediately",
|
|
476
|
+
data: {
|
|
477
|
+
role: this.role,
|
|
478
|
+
socketId: this.socket.id,
|
|
479
|
+
clientInstanceId: this._clientInstanceId,
|
|
480
|
+
},
|
|
481
|
+
timestamp: Date.now(),
|
|
482
|
+
sessionId: "debug-session",
|
|
483
|
+
runId: "run1",
|
|
484
|
+
hypothesisId: "J",
|
|
485
|
+
}),
|
|
486
|
+
},
|
|
487
|
+
).catch(() => {});
|
|
488
|
+
// #endregion
|
|
489
|
+
onConnect();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
this.socket.on("disconnect", () => {
|
|
493
|
+
this.store.getState().setStatus("disconnected");
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
this.socket.on("server:error", (payload: { message: string }) => {
|
|
497
|
+
this.store.getState().setError(payload.message);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
// DO NOT register server:state or host-specific listeners here
|
|
501
|
+
// They must be registered AFTER socket connects in the onConnect handler
|
|
502
|
+
// This is because Socket.io listeners registered before connect don't work reliably
|
|
503
|
+
|
|
504
|
+
// Controller-specific listeners
|
|
505
|
+
if (this.role === "controller") {
|
|
506
|
+
this.socket.on("server:welcome", (payload) => {
|
|
507
|
+
const store = this.store.getState();
|
|
508
|
+
store.setRoomId(payload.roomId);
|
|
509
|
+
if (payload.controllerId) {
|
|
510
|
+
store.setControllerId(payload.controllerId);
|
|
511
|
+
}
|
|
512
|
+
if (payload.player) {
|
|
513
|
+
store.upsertPlayer(payload.player);
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
this.socket.on("server:hostLeft", (payload) => {
|
|
518
|
+
const store = this.store.getState();
|
|
519
|
+
store.setError(payload.reason);
|
|
520
|
+
store.setStatus("disconnected");
|
|
521
|
+
store.resetGameState();
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Connect to the Air Jam server.
|
|
528
|
+
*/
|
|
529
|
+
public connect() {
|
|
530
|
+
// #region agent log
|
|
531
|
+
fetch("http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e", {
|
|
532
|
+
method: "POST",
|
|
533
|
+
headers: { "Content-Type": "application/json" },
|
|
534
|
+
body: JSON.stringify({
|
|
535
|
+
location: "AirJamClient.ts:177",
|
|
536
|
+
message: "connect() method called",
|
|
537
|
+
data: {
|
|
538
|
+
role: this.role,
|
|
539
|
+
socketId: this.socket.id,
|
|
540
|
+
socketConnected: this.socket.connected,
|
|
541
|
+
clientInstanceId: this._clientInstanceId,
|
|
542
|
+
},
|
|
543
|
+
timestamp: Date.now(),
|
|
544
|
+
sessionId: "debug-session",
|
|
545
|
+
runId: "run1",
|
|
546
|
+
hypothesisId: "CONNECT_METHOD",
|
|
547
|
+
}),
|
|
548
|
+
}).catch(() => {});
|
|
549
|
+
// #endregion
|
|
550
|
+
|
|
551
|
+
// CRITICAL FIX: Check if listeners were removed by destroy() and re-register them
|
|
552
|
+
// This handles React Strict Mode which calls destroy() then connect() on the same client
|
|
553
|
+
const hasConnectListener = this.socket.listeners("connect").length > 0;
|
|
554
|
+
if (!hasConnectListener) {
|
|
555
|
+
// #region agent log
|
|
556
|
+
fetch(
|
|
557
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
558
|
+
{
|
|
559
|
+
method: "POST",
|
|
560
|
+
headers: { "Content-Type": "application/json" },
|
|
561
|
+
body: JSON.stringify({
|
|
562
|
+
location: "AirJamClient.ts:182",
|
|
563
|
+
message:
|
|
564
|
+
"connect() detected listeners removed by destroy() - re-registering all listeners",
|
|
565
|
+
data: {
|
|
566
|
+
role: this.role,
|
|
567
|
+
socketId: this.socket.id,
|
|
568
|
+
clientInstanceId: this._clientInstanceId,
|
|
569
|
+
},
|
|
570
|
+
timestamp: Date.now(),
|
|
571
|
+
sessionId: "debug-session",
|
|
572
|
+
runId: "run1",
|
|
573
|
+
hypothesisId: "CONNECT_REREGISTER",
|
|
574
|
+
}),
|
|
575
|
+
},
|
|
576
|
+
).catch(() => {});
|
|
577
|
+
// #endregion
|
|
578
|
+
this.setupListeners();
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (!this.socket.connected) {
|
|
582
|
+
this.store.getState().setStatus("connecting");
|
|
583
|
+
// #region agent log
|
|
584
|
+
fetch(
|
|
585
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
586
|
+
{
|
|
587
|
+
method: "POST",
|
|
588
|
+
headers: { "Content-Type": "application/json" },
|
|
589
|
+
body: JSON.stringify({
|
|
590
|
+
location: "AirJamClient.ts:190",
|
|
591
|
+
message: "calling socket.connect()",
|
|
592
|
+
data: {
|
|
593
|
+
role: this.role,
|
|
594
|
+
socketId: this.socket.id,
|
|
595
|
+
clientInstanceId: this._clientInstanceId,
|
|
596
|
+
},
|
|
597
|
+
timestamp: Date.now(),
|
|
598
|
+
sessionId: "debug-session",
|
|
599
|
+
runId: "run1",
|
|
600
|
+
hypothesisId: "CONNECT_METHOD",
|
|
601
|
+
}),
|
|
602
|
+
},
|
|
603
|
+
).catch(() => {});
|
|
604
|
+
// #endregion
|
|
605
|
+
this.socket.connect();
|
|
606
|
+
// #region agent log
|
|
607
|
+
fetch(
|
|
608
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
609
|
+
{
|
|
610
|
+
method: "POST",
|
|
611
|
+
headers: { "Content-Type": "application/json" },
|
|
612
|
+
body: JSON.stringify({
|
|
613
|
+
location: "AirJamClient.ts:192",
|
|
614
|
+
message: "socket.connect() called - checking state",
|
|
615
|
+
data: {
|
|
616
|
+
role: this.role,
|
|
617
|
+
socketId: this.socket.id,
|
|
618
|
+
socketConnected: this.socket.connected,
|
|
619
|
+
clientInstanceId: this._clientInstanceId,
|
|
620
|
+
},
|
|
621
|
+
timestamp: Date.now(),
|
|
622
|
+
sessionId: "debug-session",
|
|
623
|
+
runId: "run1",
|
|
624
|
+
hypothesisId: "CONNECT_METHOD",
|
|
625
|
+
}),
|
|
626
|
+
},
|
|
627
|
+
).catch(() => {});
|
|
628
|
+
// #endregion
|
|
629
|
+
// CRITICAL FIX: Register listener immediately if socket connects synchronously
|
|
630
|
+
// Socket.io may connect before the 'connect' event fires, so we check immediately
|
|
631
|
+
// and also use a timeout as fallback
|
|
632
|
+
const checkAndRegister = () => {
|
|
633
|
+
if (this.socket.connected) {
|
|
634
|
+
const hasListener = this.socket.listeners("server:state").length > 0;
|
|
635
|
+
// #region agent log
|
|
636
|
+
fetch(
|
|
637
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
638
|
+
{
|
|
639
|
+
method: "POST",
|
|
640
|
+
headers: { "Content-Type": "application/json" },
|
|
641
|
+
body: JSON.stringify({
|
|
642
|
+
location: "AirJamClient.ts:200",
|
|
643
|
+
message:
|
|
644
|
+
"checkAndRegister: socket connected, checking listener",
|
|
645
|
+
data: {
|
|
646
|
+
role: this.role,
|
|
647
|
+
socketId: this.socket.id,
|
|
648
|
+
hasListener,
|
|
649
|
+
clientInstanceId: this._clientInstanceId,
|
|
650
|
+
},
|
|
651
|
+
timestamp: Date.now(),
|
|
652
|
+
sessionId: "debug-session",
|
|
653
|
+
runId: "run1",
|
|
654
|
+
hypothesisId: "CONNECT_METHOD",
|
|
655
|
+
}),
|
|
656
|
+
},
|
|
657
|
+
).catch(() => {});
|
|
658
|
+
// #endregion
|
|
659
|
+
if (!hasListener) {
|
|
660
|
+
// #region agent log
|
|
661
|
+
fetch(
|
|
662
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
663
|
+
{
|
|
664
|
+
method: "POST",
|
|
665
|
+
headers: { "Content-Type": "application/json" },
|
|
666
|
+
body: JSON.stringify({
|
|
667
|
+
location: "AirJamClient.ts:204",
|
|
668
|
+
message:
|
|
669
|
+
"socket connected but listener missing - registering now",
|
|
670
|
+
data: {
|
|
671
|
+
role: this.role,
|
|
672
|
+
socketId: this.socket.id,
|
|
673
|
+
clientInstanceId: this._clientInstanceId,
|
|
674
|
+
},
|
|
675
|
+
timestamp: Date.now(),
|
|
676
|
+
sessionId: "debug-session",
|
|
677
|
+
runId: "run1",
|
|
678
|
+
hypothesisId: "CONNECT_METHOD",
|
|
679
|
+
}),
|
|
680
|
+
},
|
|
681
|
+
).catch(() => {});
|
|
682
|
+
// #endregion
|
|
683
|
+
this.store.getState().setStatus("connected");
|
|
684
|
+
this.store.getState().setError(undefined);
|
|
685
|
+
this.setupServerStateListener();
|
|
686
|
+
this.setupHostListeners();
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
// Check immediately (in case socket connected synchronously)
|
|
691
|
+
checkAndRegister();
|
|
692
|
+
// Also check after a short delay (in case connection happens asynchronously)
|
|
693
|
+
setTimeout(checkAndRegister, 50);
|
|
694
|
+
setTimeout(checkAndRegister, 200);
|
|
695
|
+
setTimeout(checkAndRegister, 500);
|
|
696
|
+
} else {
|
|
697
|
+
// #region agent log
|
|
698
|
+
fetch(
|
|
699
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
700
|
+
{
|
|
701
|
+
method: "POST",
|
|
702
|
+
headers: { "Content-Type": "application/json" },
|
|
703
|
+
body: JSON.stringify({
|
|
704
|
+
location: "AirJamClient.ts:218",
|
|
705
|
+
message: "connect() called but socket already connected",
|
|
706
|
+
data: {
|
|
707
|
+
role: this.role,
|
|
708
|
+
socketId: this.socket.id,
|
|
709
|
+
clientInstanceId: this._clientInstanceId,
|
|
710
|
+
},
|
|
711
|
+
timestamp: Date.now(),
|
|
712
|
+
sessionId: "debug-session",
|
|
713
|
+
runId: "run1",
|
|
714
|
+
hypothesisId: "CONNECT_METHOD",
|
|
715
|
+
}),
|
|
716
|
+
},
|
|
717
|
+
).catch(() => {});
|
|
718
|
+
// #endregion
|
|
719
|
+
// Ensure listeners are registered if socket is already connected
|
|
720
|
+
if (!this.socket.listeners("server:state").length) {
|
|
721
|
+
// #region agent log
|
|
722
|
+
fetch(
|
|
723
|
+
"http://127.0.0.1:7245/ingest/77275639-c0f5-41c0-a729-c2568f3ab68e",
|
|
724
|
+
{
|
|
725
|
+
method: "POST",
|
|
726
|
+
headers: { "Content-Type": "application/json" },
|
|
727
|
+
body: JSON.stringify({
|
|
728
|
+
location: "AirJamClient.ts:222",
|
|
729
|
+
message:
|
|
730
|
+
"socket already connected but listener missing - registering now",
|
|
731
|
+
data: {
|
|
732
|
+
role: this.role,
|
|
733
|
+
socketId: this.socket.id,
|
|
734
|
+
clientInstanceId: this._clientInstanceId,
|
|
735
|
+
},
|
|
736
|
+
timestamp: Date.now(),
|
|
737
|
+
sessionId: "debug-session",
|
|
738
|
+
runId: "run1",
|
|
739
|
+
hypothesisId: "CONNECT_METHOD",
|
|
740
|
+
}),
|
|
741
|
+
},
|
|
742
|
+
).catch(() => {});
|
|
743
|
+
// #endregion
|
|
744
|
+
this.store.getState().setStatus("connected");
|
|
745
|
+
this.store.getState().setError(undefined);
|
|
746
|
+
this.setupServerStateListener();
|
|
747
|
+
this.setupHostListeners();
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Disconnect from the Air Jam server.
|
|
754
|
+
*/
|
|
755
|
+
public disconnect() {
|
|
756
|
+
this.socket.disconnect();
|
|
757
|
+
this.store.getState().setStatus("disconnected");
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Get the current input buffer.
|
|
762
|
+
*/
|
|
763
|
+
public get inputBuffer() {
|
|
764
|
+
return this._inputBuffer;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* Cleanup resources.
|
|
769
|
+
*/
|
|
770
|
+
public destroy() {
|
|
771
|
+
this.disconnect();
|
|
772
|
+
this.socket.removeAllListeners();
|
|
773
|
+
this._inputBuffer.clear();
|
|
774
|
+
}
|
|
775
|
+
}
|