Asobiai SDK

Online multiplayer

Join a room with AIGame.joinRoom and play with others in real time. The server just relays messages to everyone in the room, so keep the game rules in the game (run them on the host).

v2.7.0Check it from your game with window.AIGame?.version.Changelog

漫画图解 在线对战
  1. Asobi:“一个人玩也不错,但好想和别人对战呀。”
  2. Ai:“只要调用这个,就能进入在线对战的房间!”(AIGame.joinRoom())
  3. Asobi:“用口令,就能和朋友进同一个房间呢。”
  4. Asobi:“赢啦~!” Ai:“再来一局!下次我可不会输。”

Usage

js
// Join a room with free seats (quick match, 2-16 players)
const room = await window.AIGame.joinRoom({ maxPlayers: 2 });
// To play with friends, pass a shared code: joinRoom({ maxPlayers: 2, code: "ABCD" })

room.me;        // you { id, name }
room.players;   // everyone in the room (including you)
room.isHost;    // true for whoever joined first (let the host run the rules)

room.on("join", (player) => { /* someone joined */ });
room.on("leave", (player) => { /* someone left */ });
room.on("message", (data, from) => { /* a message from another player */ });

room.send({ type: "move", x: 3, y: 1 });   // to everyone else
room.send({ type: "hello" }, player.id);    // to one player
room.send({ type: "team" }, [a.id, b.id]);  // to several players (counts as one message)
room.leave();
  • Works only while the game is played on an Asobiai game page (joinRoom fails in embeds on other sites or when the file is opened directly).
  • Logged-in players appear with their account name; guests get names like “Guest 1234”.
  • Up to 16 players per room, 16 KB per message and 60 messages per second per player (extra ones are dropped; a message to several players counts as one). Send inputs or a few updates per second rather than every frame.
  • On the game's edit page, choose Versus (players compete) or Party (many players / co-op) under “Online” and set the player count. It shows on the game page and cards, like “Online party · 4–8 players”, and the game list can be filtered by it.
  • The server only relays messages, so it can't fully prevent cheating. It's meant for casual play.

Reconnecting, room state and compatibility

When the connection drops (phone lock, network switch), players return to the same seat automatically (closing the tab counts as leaving right away). The host can save the room state on the server, so the next host can continue if the host leaves. Per-player secrets (roles, hands) can be read only by that player and the host.

js
// Options when joining
const room = await window.AIGame.joinRoom({
  maxPlayers: 8,
  code: "ABCD",
  protocol: "3",     // compatibility version: players with a different one can't join (bump it when your messages change)
  reconnect: 90,     // seconds to keep the seat and reconnect automatically after a drop (0-300, default 60)
});

// Connection events
room.on("reconnecting", () => { /* your connection dropped; reconnecting */ });
room.on("reconnected", () => { /* back; call room.loadState() for the latest state */ });
room.on("disconnected", (reason) => { /* could not come back ("timeout" / "expired") */ });
room.on("away", (player) => { /* another player dropped (their seat is kept) */ });
room.on("back", (player) => { /* they came back */ });
room.on("leave", (player, reason) => { /* seat released ("left" = they left / "timeout" = they did not return) */ });
room.on("host", (player) => { /* the host changed; if it's you, loadState() and continue */ });

// Save the state (host only). players = per-player secrets (only that player and the host can read them), host = host only
await room.saveState({
  public: { round: 3, scores },
  host: { answer, deck },
  players: { [aliceId]: { role: "spy" }, [bobId]: { role: "detective" } },
});

// Read: your part (mine) and the shared part (public); the host also gets host and players
const { generation, public: pub, mine, host } = await room.loadState();
  • State is up to 256 KB, saved up to 5 times a second per room, and kept for 10 minutes after everyone leaves. Saves with an older generation are refused, so an old host can't roll the state back.
  • The host's browser runs the game, so secrets can't be fully hidden from the host's device. It's meant for casual play.
  • Scores sent while in a room go to a separate “Online” leaderboard (see the Leaderboards page).
What to tell your AI

Make it an online multiplayer game. Join with window.AIGame.joinRoom({ maxPlayers: 2 }), send with room.send(data), receive with room.on("message", (data, from) => …), and track people with room.on("join") / room.on("leave"). Let the player with room.isHost === true start the game and decide results. Also support playing with friends via joinRoom({ maxPlayers: 2, code }). Send only inputs or state changes, not every frame. When publishing, set online_modes with the player count for each way to play (e.g. [{"mode":"party","min":2,"max":8},{"mode":"versus","min":4,"max":8}] for co-op 2–8 and versus 4–8). Players return to the same seat after a drop: on room.on("reconnected") and room.on("host") call room.loadState() and continue, and have the host save progress with room.saveState({ public, host, players }). Bump the joinRoom protocol when the message format changes. Then call check_game to catch errors and images that fail to load.