42_ft_transcendence/frontend/static/js/api/Matchmaking.js
2024-03-31 10:59:40 +02:00

67 lines
1.2 KiB
JavaScript

import { Client } from "./Client.js";
class MatchMaking
{
/**
* @param {Client} client
*/
constructor(client)
{
/**
* @type {Client}
*/
this.client = client;
this.searching = false;
}
/**
*
* @param {CallableFunction} receive_func
* @param {CallableFunction} disconnect_func
* @param {Number} mode The number of players in a game
* @returns {Promise<?>}
*/
async start(receive_func, disconnect_func, mode, gamemode)
{
if (!await this.client.isAuthenticated())
return null;
this.gamemode = gamemode
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/matchmaking/${mode}`;
this._socket = new WebSocket(url);
this.searching = true;
this.receive_func = receive_func;
this.disconnect_func = disconnect_func;
this._socket.onmessage = function (event) {
const data = JSON.parse(event.data);
receive_func(data);
};
this._socket.onclose = this.onclose.bind(this);
}
onclose(event)
{
this.stop();
this.disconnect_func(event);
}
/**
* @returns {Promise<?>}
*/
async stop()
{
if (this._socket)
this._socket.close();
this._socket = undefined;
this.searching = false;
}
}
export {MatchMaking};