Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
script.js 1.47 KiB
import Signaler from "./Signaler.js";
import SkribblContainer from "./SkribblContainer.js";

document.addEventListener("DOMContentLoaded",async()=>{
	if (document.location.hash){
		document.body.innerHTML = "";
		const gameID = document.location.hash.substring(1);
		const {connection,dataChannel} = await Signaler.join(gameID);
		console.group("Joined game "+gameID+"!");
		console.log("connection:",connection);
		console.log("dataChannel:",dataChannel);
		console.groupEnd();
		dataChannel.onmessage = (e)=>{
			console.log("Message through DataChannel:",e.data);
			dataChannel.send("Got ya message, many thanks <3");
		}
	}else{
		/** @type {HTMLButtonElement} *///@ts-ignore
		const button = document.getElementById("button");
		await new Promise(resolve=>{button.addEventListener("click",async e=>{
			button.disabled = true;
			resolve();
		})});
		let id = await Signaler.host(async({connection,dataChannel})=>{
			console.group("New connection!");
			console.log("connection:",connection);
			console.log("dataChannel:",dataChannel);
			console.groupEnd();
			dataChannel.onmessage = (e)=>{
				console.log("Message through DataChannel:",e.data);
			}
			if (dataChannel.readyState!="open"){
				await new Promise(resolve=>{
					dataChannel.onopen = resolve;
				});
			}
			dataChannel.send("Oh hello there :3");
		});
		alert(document.location.host+document.location.pathname+"#"+id);
	}
	const game = new SkribblContainer();
	document.body.innerHTML = "";
	document.body.appendChild(game);
});