${translateText("matchmaking_modal.limit_reached")}
${translateText("matchmaking_modal.limit_reached_info")}
`;
}
if (!this.connected) {
return this.renderLoadingSpinner(
translateText("matchmaking_modal.connecting"),
"blue",
);
}
if (this.gameID === null) {
return this.renderLoadingSpinner(
translateText("matchmaking_modal.searching"),
"green",
);
} else {
return this.renderLoadingSpinner(
translateText("matchmaking_modal.waiting_for_game"),
"yellow",
);
}
}
private openSubscriptions = () => {
// The matchmaking modal isn't registered with the modal router, so it
// won't be closed by the store opening from the hash change.
this.close();
window.location.hash = "modal=store&tab=subscriptions";
};
private async connect() {
// A pending join timer from a previous socket must not fire on this one.
if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
this.connectTimeout = null;
}
this.socket = new WebSocket(
`${ClientEnv.jwtIssuer()}/matchmaking/join?instance_id=${encodeURIComponent(ClientEnv.instanceId())}&mode=${this.mode}`,
);
this.socket.onopen = async () => {
console.log("Connected to matchmaking server");
this.connectTimeout = setTimeout(async () => {
if (this.socket?.readyState !== WebSocket.OPEN) {
console.warn("[Matchmaking] socket not ready");
return;
}
// Set a delay so the user can see the "connecting" message,
// otherwise the "searching" message will be shown immediately.
// Also wait so people who back out immediately aren't added
// to the matchmaking queue.
this.socket.send(
JSON.stringify({
type: "join",
jwt: await getPlayToken(),
}),
);
this.connected = true;
this.requestUpdate();
}, 2000);
};
this.socket.onmessage = (event) => {
console.log(event.data);
const data = JSON.parse(event.data);
if (data.type === "match-assignment") {
this.intentionalClose = true;
this.socket?.close();
console.log(`matchmaking: got game ID: ${data.gameId}`);
this.gameID = data.gameId;
this.gameCheckInterval = setInterval(() => this.checkGame(), 1000);
}
};
this.socket.onerror = (event: Event) => {
console.error("WebSocket error occurred:", event);
};
this.socket.onclose = (event: CloseEvent) => {
console.log(
`Matchmaking server closed connection: code=${event.code} reason=${event.reason}`,
);
if (this.intentionalClose || this.gameID !== null) {
return;
}
// 1008 is also used for auth failures ("Invalid session"), so match on
// the reason. Out of free ranked plays — the server will keep refusing
// until the next UTC day (or a subscription), so don't reconnect.
if (event.code === 1008 && event.reason === "ranked_limit_reached") {
this.connected = false;
this.limitReached = true;
return;
}
if (event.code === 1000) {
// A newer connection for this account (e.g. a second tab) took the
// queue slot; this socket was replaced. Do not retry.
window.dispatchEvent(
new CustomEvent("show-message", {
detail: {
message: translateText("matchmaking_modal.replaced"),
color: "red",
duration: 5000,
},
}),
);
this.close();
return;
}
// 1008: the jwt was rejected — getPlayToken() refreshes expired tokens,
// so rejoining sends a fresh one. Anything else is a server
// restart/deploy; the queue is in-memory only, so rejoin. Back off in
// case the failure repeats.
this.connected = false;
const delay = Math.min(1000 * 2 ** this.reconnectAttempts++, 15000);
this.reconnectTimeout = setTimeout(() => this.connect(), delay);
};
}
protected async onOpen(): Promise