-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
146 lines (126 loc) · 5.2 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
let game_keyup = null;
let game_keydown = null;
window.RufflePlayer = window.RufflePlayer || {};
var callIntervalId = null;
var guest_video_id = null;
var guest_data_id = null;
function on_host_load() {
const ruffle = window.RufflePlayer.newest();
const player = ruffle.createPlayer();
const container = document.getElementById("container");
player.style.width = "100%";
player.style.height = "800px";
container.appendChild(player);
player.load("tank-trouble.swf");
const peer = new Peer();
console.log("peer=", peer);
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
let conn = peer.connect(guest_data_id);
conn.on('open', function() {
console.log("Keyboard connection established");
// Receive messages
conn.on('data', function(data) {
console.log("received data", data);
window.dispatchEvent(new KeyboardEvent(data["type"], {
code: data['code'],
}));
});
});
});
const videopeer = new Peer();
callIntervalId = setInterval(function(p) {
const canvasElt = document.querySelector("ruffle-player")?.shadowRoot.querySelector("canvas");
if (canvasElt != null) {
console.log("Canvas exists, setting up call now");
const stream = canvasElt.captureStream(30); // FPS
const video_track = stream.getVideoTracks()[0];
video_track.contentHint = "motion";
var call = p.call(guest_video_id, stream);
console.log("stream=", stream);
// Disabled, we'll re-enable this for lag-compensation
// document.getElementById("receiving-video").srcObject = stream;
// document.getElementById("receiving-video").play();
clearInterval(callIntervalId);
} else {
console.log("canvas still null");
}
}, 1000, videopeer);
}
function transmitKeystroke(conn, type, event) {
console.log("transmitting ", type, event);
conn.send({type: type, code: event.code});
}
var displayPeerIdIntervalId = null;
function on_guest_load() {
const peer = new Peer();
console.log("peer=", peer);
peer.on('open', function(id) {
console.log('Opened, data peer ID is: ' + id);
guest_data_id = id;
});
peer.on('connection', function(conn) {
document.getElementById("connectiondetails").innerHTML = "";
conn.on('open', function() {
console.log("Keyboard connection established");
document.addEventListener("keyup", function(ev) {transmitKeystroke(conn, "keyup", ev)});
document.addEventListener("keydown", function(ev) {transmitKeystroke(conn, "keydown", ev)});
});
});
const videopeer = new Peer();
videopeer.on('open', function(id) {
console.log('Opened, video peer ID is: ' + id);
guest_video_id = id;
})
videopeer.on('call', function(call) {
console.log("received call");
call.on('stream', function(stream) {
console.log("On stream, setting video element to ", stream);
const video_track = stream.getVideoTracks()[0];
video_track.contentHint = "motion";
document.getElementById("receiving-video").srcObject = stream;
document.getElementById("receiving-video").play();
});
call.answer();
});
displayPeerIdIntervalId = setInterval(function() {
if (guest_data_id != null && guest_video_id != null) {
let combinedID = `${guest_data_id}/${guest_video_id}`
document.getElementById("connectiondetails").innerHTML =
`<h1>Connection Information</h1><p>Please pass your connection ID
<input id="connectionid" readonly size="${combinedID.length}" value="${combinedID}"> to the host.
The game will automatically start when the host clicks the 'Start game' button`
clearInterval(displayPeerIdIntervalId);
} else {
console.log("still null");
}
}, 200);
}
function submit_host_id() {
let guest_combined_id = document.getElementById("guest_combined_id").value.trim();
if (guest_combined_id.length == 73) {
guest_data_id = guest_combined_id.split('/')[0];
guest_video_id = guest_combined_id.split('/')[1];
on_host_load();
document.getElementById("connectiondetails").innerHTML = '';
} else {
document.getElementById("error-connectiondetails").innerText = "An error happened";
}
}
function click_host() {
document.getElementById("hostguestchoice").remove();
document.getElementById("connectiondetails").innerHTML = `
<h1>Host</h1>
<p>Please paste the ID you received from the guest</p>
<input id="guest_combined_id" size="73">
<div class="button-row"><button onclick="submit_host_id()">Start game</button></div>
<div id="error-connectiondetails"></div>
`
}
function click_guest() {
document.getElementById("hostguestchoice").remove();
document.getElementById("connectiondetails").innerHTML =
"<h1>Connection Information</h1><p>Connecting…</p>";
on_guest_load();
}