2023-12-16 12:00:38 -05:00
|
|
|
import { Client } from "./client.js";
|
2023-12-12 06:06:13 -05:00
|
|
|
|
|
|
|
class MatchMaking
|
|
|
|
{
|
|
|
|
/**
|
2023-12-16 12:00:38 -05:00
|
|
|
* @param {Client} client
|
2023-12-12 06:06:13 -05:00
|
|
|
*/
|
|
|
|
constructor(client)
|
|
|
|
{
|
|
|
|
/**
|
2023-12-20 13:10:26 -05:00
|
|
|
* @type {Client}
|
2023-12-12 06:06:13 -05:00
|
|
|
*/
|
|
|
|
this.client = client
|
2023-12-23 06:54:33 -05:00
|
|
|
this.searching = false;
|
2023-12-12 06:06:13 -05:00
|
|
|
}
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
async start(receive_func, disconnect_func, mode)
|
2023-12-12 06:06:13 -05:00
|
|
|
{
|
|
|
|
if (!await this.client.isAuthentificate())
|
|
|
|
return null;
|
|
|
|
|
2023-12-26 08:28:04 -05:00
|
|
|
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/matchmaking/${mode}`;
|
2023-12-25 19:03:09 -05:00
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
this._socket = new WebSocket(url);
|
2023-12-12 06:06:13 -05:00
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
this.searching = true;
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
this.receive_func = receive_func;
|
|
|
|
this.disconnect_func = disconnect_func;
|
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
this._socket.onmessage = function (event) {
|
2023-12-12 06:06:13 -05:00
|
|
|
const data = JSON.parse(event.data);
|
2023-12-23 10:47:22 -05:00
|
|
|
receive_func(data);
|
2023-12-12 06:06:13 -05:00
|
|
|
};
|
2023-12-23 10:47:22 -05:00
|
|
|
|
|
|
|
this._socket.onclose = this.onclose.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
onclose(event)
|
|
|
|
{
|
|
|
|
this.stop();
|
2023-12-23 12:47:16 -05:00
|
|
|
this.disconnect_func(event);
|
2023-12-12 06:06:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop()
|
|
|
|
{
|
2023-12-23 06:54:33 -05:00
|
|
|
this.searching = false;
|
|
|
|
this._socket.close()
|
2023-12-12 06:06:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 19:03:09 -05:00
|
|
|
export {MatchMaking}
|