Skip to content
Snippets Groups Projects
SkribblClient.js 770 B
Newer Older
import DataChannel from "./networking/DataChannel.js";
import Signaler from "./networking/Signaler.js";

export default class SkribblClient {
	/**
	 * Constructs a new client for a given game ID or DataChannel.
	 * @param {string|DataChannel} idOrDataChannel
	 */
	constructor(idOrDataChannel){
		this._readyPromise = (async()=>{
			this._dataChannel = idOrDataChannel instanceof DataChannel?idOrDataChannel:await Signaler.join(idOrDataChannel);
			await this._dataChannel.waitUntilReady();
			this._dataChannel.send("Hi there :3");
			this._dataChannel.onMessage(message=>{
				console.log(`message from server: "${message}"`);
			})
		})();
	}

	/**
	 * Waits until the connection to the server is ready.
	 */
	async waitUntilReady(){
		return this._readyPromise;
	}
}