Skip to content
Snippets Groups Projects
script.js 1.29 KiB
Newer Older
import Signaler from "./Signaler.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");
		button.addEventListener("click",async e=>{
			button.disabled = true;
			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);
		});
	}