var jwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYyMTYzYjUxZjg5ZTM0MWE1ZTMwZTBjZCIsInJvbGUiOiJBZG1pbiIsImlhdCI6MTY0NTYzMjM2MiwiZXhwIjoxNjU0MDEzMTYyfQ.uCvHGWUSLBSkbYz_Pp-ZK7ZaSb5asW2gr19omb23qow"; const SOCKET_URL = "http://localhost:3000"; const EVENT_ID = "Event_id"; const GAME_ID = "sidescroller"; var MY_SOCKET_ID = ""; var InitializeSockify = (params) => { var socket_url = params.url || SOCKET_URL; var event_id = params.event_id || EVENT_ID; var room_id = `${event_id}:${GAME_ID}`; // NOTE("Justin"): Wrapping direct c functions we can call from our incoming socket calls vs the traditional "SendMessage" way. This way is up to 10x faster c_game_event = finalUnityInstance.Module.cwrap("call_cb_game_event", null, ["string"]); c_game_update = finalUnityInstance.Module.cwrap("call_cb_game_update", null, ["string"]); var socket = io(socket_url, { auth: { jwt_token: params.jwt_token || jwtToken, user: params.user || {username: "Justin", role: "admin", thumbnail: "https://picsum.photos/300"}, }, }); // ****** LOCAL socket.on("connect", () => { MY_SOCKET_ID = socket.id; console.log(socket.id); var users = params.users || [ {username: "Justin", role: "admin", thumbnail: "https://picsum.photos/300"}, {username: "Kareem", role: "basic", thumbnail: "https://picsum.photos/300"}, ]; var join_data = { id: room_id, users: users, config: params.config || {headlessMode: true}, }; socket.emit("join_room", join_data); }); socket.on("disconnect", () => { console.log(socket.id); }); socket.on("connect_error", (err) => { console.log(err instanceof Error); // true console.log(err.message); // not authorized if (err.data) console.log(err.data); // { content: "Please retry later" } }); // ****** ROOM BROADCASTS socket.on("user_joined", (data) => { console.log("user_joined"); console.log(JSON.stringify(data, null, 2)); finalUnityInstance.SendMessage("Sockify", "Game_User_Join", JSON.stringify(data)); }); socket.on("user_left", (data) => { console.log("user_left"); console.log(JSON.stringify(data, null, 2)); finalUnityInstance.SendMessage("Sockify", "Game_User_Left", JSON.stringify(data)); }); // GAME SPECIFIC socket.on("game_update", (updateData) => { //console.log(JSON.stringify(updateData, null, 2)); //console.time("game_update-C"); c_game_update(updateData); //console.timeEnd("game_update-C"); }); socket.on("game_event", (eventData) => { //console.log(JSON.stringify(eventData, null, 2)); c_game_event(eventData); }); socket.on("game_init", (initData) => { console.log(JSON.stringify(initData, null, 2)); finalUnityInstance.SendMessage("Sockify", "Game_Init", JSON.stringify(initData)); }); window.SockifyParentEvent = (eventData) => { c_game_event(eventData); // NOTE("Justin"): JSON STRING THAT DESERIALIZES INTO A C# class Event_Data { public GameEventType type; public string input; } }; window.EmitSockifySetState = (input) => { console.log(`EmitSockifySetState ==> input: ${input}`); // NOTE("Justin"): this emit message will go directly to the specific game's listeners it has set up in its "set_handlers()" function IE. (standup_spinner.js ON SERVER SIDE) socket.emit("game_set_state", input); if (window.parent && window.parent != window) { window.parent.postMessage({type: "state", data: input}, "*"); } }; window.EmitSockifyEvent = (eventId, input) => { console.log(`EmitSockifyEvent ==> input: ${input}`); console.log(`EmitSockifyEvent ==> eventId: ${eventId}`); // NOTE("Justin"): this emit message will go directly to the specific game's listeners it has set up in its "set_handlers()" function IE. (standup_spinner.js ON SERVER SIDE) socket.emit(eventId, input); if (window.parent && window.parent != window) { window.parent.postMessage({type: "event", data: input}, "*"); } }; };