ft_transcendence/frontend/static/js/api/matchmaking.js

41 lines
646 B
JavaScript

import { Client } from "./client.js";
class MatchMaking
{
/**
* @param {Client} client
*/
constructor(client)
{
/**
* @type {Client}
*/
this.client = client
this.searching = false;
}
async start(func, mode)
{
if (!await this.client.isAuthentificate())
return null;
let url = `wss://${window.location.host}/ws/matchmaking/${mode}`;
this._socket = new WebSocket(url);
this.searching = true;
this._socket.onmessage = function (event) {
const data = JSON.parse(event.data);
func(data.game_id)
};
}
async stop()
{
this.searching = false;
this._socket.close()
}
}
export {MatchMaking}