|
|
|
@ -16,16 +16,16 @@ type LatLng = {
|
|
|
|
|
type Peer = {
|
|
|
|
|
marker: Marker | null;
|
|
|
|
|
dataChannel: RTCDataChannel;
|
|
|
|
|
listItemEl: HTMLElement;
|
|
|
|
|
statusEl: HTMLElement;
|
|
|
|
|
};
|
|
|
|
|
const init = async () => {
|
|
|
|
|
const peers = new Map<PeerID, Peer>();
|
|
|
|
|
const onPeerConnected = (peerID: PeerID, dataChannel: RTCDataChannel) => {
|
|
|
|
|
const peer = {
|
|
|
|
|
marker: null,
|
|
|
|
|
dataChannel,
|
|
|
|
|
} as Peer;
|
|
|
|
|
peers.set(peerID, peer);
|
|
|
|
|
|
|
|
|
|
const onPeerConnected = (
|
|
|
|
|
peerID: PeerID,
|
|
|
|
|
rtc: RTCPeerConnection,
|
|
|
|
|
dataChannel: RTCDataChannel,
|
|
|
|
|
) => {
|
|
|
|
|
dataChannel.addEventListener("message", (msg) => {
|
|
|
|
|
const latLng: LatLng = JSON.parse(msg.data);
|
|
|
|
|
|
|
|
|
@ -45,6 +45,21 @@ const init = async () => {
|
|
|
|
|
dataChannel.send(JSON.stringify(lastPosition));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const statusEl = swel("div", { className: "peerStatus" }, "Connected");
|
|
|
|
|
const listItemEl = swel("div", { className: "peer" }, [
|
|
|
|
|
swel("div", { className: "peerID" }, peerID),
|
|
|
|
|
statusEl,
|
|
|
|
|
]);
|
|
|
|
|
peersList.appendChild(listItemEl);
|
|
|
|
|
|
|
|
|
|
const peer = {
|
|
|
|
|
marker: null,
|
|
|
|
|
dataChannel,
|
|
|
|
|
listItemEl,
|
|
|
|
|
statusEl,
|
|
|
|
|
} as Peer;
|
|
|
|
|
peers.set(peerID, peer);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const net = await initNetworking(onPeerConnected);
|
|
|
|
@ -63,7 +78,9 @@ const init = async () => {
|
|
|
|
|
"Connect",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const controls = swel("div", { id: "controls" }, [
|
|
|
|
|
const peersList = swel("div", { id: "peers" });
|
|
|
|
|
const lobby = swel("div", { id: "lobby" }, [
|
|
|
|
|
peersList,
|
|
|
|
|
swel("div", { id: "peerID" }, net.peerID),
|
|
|
|
|
swel("div", [peerIDInput, connectButton]),
|
|
|
|
|
]);
|
|
|
|
@ -71,7 +88,7 @@ const init = async () => {
|
|
|
|
|
const mapContainer = swel("div", { id: "map" });
|
|
|
|
|
|
|
|
|
|
const main = document.getElementsByTagName("main")[0];
|
|
|
|
|
main.replaceChildren(controls, mapContainer);
|
|
|
|
|
main.replaceChildren(mapContainer, lobby);
|
|
|
|
|
|
|
|
|
|
const map = createMap(mapContainer);
|
|
|
|
|
map.setView([37.8, -96], 4);
|
|
|
|
|