-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.html
201 lines (157 loc) · 4.59 KB
/
index.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<title>Parsec Web Client</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="video-container">
<video></video>
</div>
<table id='server-list'></table>
<script type='module'>
import {Client} from './client.js';
/*** API ***/
async function auth(email, password) {
const res = await fetch('https://parsecgaming.com/v1/auth', {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
password,
}),
});
return await res.json();
}
async function serverList(sessionId) {
const res = await fetch('https://parsecgaming.com/v1/server-list?include_managed=true', {
method: 'get',
headers: {
'X-Parsec-Session-Id': sessionId,
},
});
return await res.json();
}
/*** CLIENT ***/
function toggleFullscreen(element) {
if (document.webkitFullscreenElement) {
document.webkitExitFullscreen();
} else {
element.webkitRequestFullscreen();
if (navigator.keyboard && navigator.keyboard.lock)
navigator.keyboard.lock();
}
}
function runClient(element, sessionId, serverId) {
return new Promise((resolve) => {
//set up client object with an event callback: gets connect, status, chat, and shutter events
const client = new Client(element, (event) => {
console.log('EVENT', event);
if (event.type === 'exit') {
document.removeEventListener('keydown', hotkeys, true);
resolve(event.code);
}
});
//set up useful hotkeys that call client methods: destroy can also be used to cancel pending connection
const hotkeys = (event) => {
event.preventDefault();
if (event.code === 'Backquote' && event.ctrlKey && event.altKey) {
client.destroy(0);
} else if (event.code === 'KeyW' && event.ctrlKey && event.altKey) {
toggleFullscreen(element);
}
};
document.addEventListener('keydown', hotkeys, true);
client.connect(sessionId, serverId, {
encoder_bitrate: 12,
});
});
}
/*** UI HELPERS ***/
function addRow(table) {
const tr = document.createElement('tr');
table.appendChild(tr);
return tr;
}
function addTextCol(tr, text) {
const td = document.createElement('td');
td.textContent = text;
tr.appendChild(td);
}
function addButtonCol(tr, text, onclick) {
const td = document.createElement('td');
tr.appendChild(td);
const button = document.createElement('button');
button.textContent = text;
button.onclick = onclick;
td.appendChild(button);
}
function addInputCol(tr, id, type, onenter) {
const td = document.createElement('td');
tr.appendChild(td);
const input = document.createElement('input');
input.id = id;
input.type = type;
input.onkeyup = (event) => {
if (event.keyCode === 13)
onenter();
};
td.appendChild(input);
}
/*** MAIN ***/
async function serverTable(sessionId) {
const json = await serverList(sessionId);
const table = document.querySelector('#server-list');
for (const server of json) {
const tr = addRow(table);
addTextCol(tr, server.name);
addTextCol(tr, server.build);
addTextCol(tr, server.server_id);
addButtonCol(tr, 'Connect', async () => {
const container = document.querySelector('.video-container');
const element = document.querySelector('video');
table.style.display = 'none';
container.style.display = 'block';
const code = await runClient(element, sessionId, server.server_id);
table.style.display = '';
container.style.display = '';
element.src = '';
element.load();
if (code !== 0)
alert(`Exit code: ${code}`);
});
}
const tr = addRow(table);
addButtonCol(tr, 'Logout', () => {
delete sessionStorage.sessionId;
window.location.reload();
});
}
function authTable() {
async function submit() {
const email = document.querySelector('#email').value;
const password = document.querySelector('#password').value;
const json = await auth(email, password);
sessionStorage.sessionId = json.session_id;
window.location.reload();
}
const table = document.querySelector('#server-list');
let tr = addRow(table);
addTextCol(tr, 'Email');
addInputCol(tr, 'email', 'email', submit);
tr = addRow(table);
addTextCol(tr, 'Password');
addInputCol(tr, 'password', 'password', submit);
document.querySelector('#email').focus();
}
if (!sessionStorage.sessionId) {
authTable();
} else {
serverTable(sessionStorage.sessionId);
}
</script>
</body>
</html>