Multiplayer client relay bridge and match lobby menu: WebSocket bridge, match lobby page, order relay, confirmed turns, checksums, chat, abort/resume
This commit is contained in:
1 parent
848ab0f677
commit
583c3fcdaf
9 files changed
+1063
-6
No files matched your search
@@ -9,6 +9,8 @@
|
||||
var httpNext = 1;
|
||||
var token = ""; // login session of the savegame service (sent with every request)
|
||||
var fetched = { state: 0 }; // download of a file from the server: 0 idle, 1 running, 2 done, 3 failed
|
||||
var ws = {}; // socket id -> { state, queue, sock }
|
||||
var wsNext = 1;
|
||||
|
||||
function bytes(ptr, len) {
|
||||
return new Uint8Array(wasm_memory.buffer, ptr, len);
|
||||
@@ -120,6 +122,50 @@
|
||||
bytes(ptr, r.body.length).set(r.body);
|
||||
delete http[id];
|
||||
};
|
||||
// WebSocket for multiplayer matches. state: 0 connecting, 1 open, 2 closed, 3 failed, 4 gone
|
||||
env.dc_ws_connect = function (up, ul) {
|
||||
var url = str(up, ul);
|
||||
if (url.charAt(0) === "/") {
|
||||
var proto = window.location.protocol === "https:" ? "wss://" : "ws://";
|
||||
url = proto + window.location.host + url;
|
||||
}
|
||||
var id = wsNext++;
|
||||
var s = { state: 0, queue: [], sock: null };
|
||||
ws[id] = s;
|
||||
try {
|
||||
s.sock = new WebSocket(url);
|
||||
s.sock.onopen = function () { s.state = 1; };
|
||||
s.sock.onmessage = function (e) {
|
||||
if (typeof e.data === "string" && s.queue.length < 2000) s.queue.push(e.data);
|
||||
};
|
||||
s.sock.onclose = function () { s.state = 2; };
|
||||
s.sock.onerror = function () { s.state = 3; };
|
||||
} catch (e) {
|
||||
s.state = 3;
|
||||
}
|
||||
return id;
|
||||
};
|
||||
env.dc_ws_state = function (id) {
|
||||
var s = ws[id];
|
||||
return s ? s.state : 4;
|
||||
};
|
||||
env.dc_ws_send = function (id, pp, pl) {
|
||||
var s = ws[id];
|
||||
if (!s || s.state !== 1 || !s.sock) return 0;
|
||||
try { s.sock.send(str(pp, pl)); return 1; } catch (e) { return 0; }
|
||||
};
|
||||
env.dc_ws_take = function (id) {
|
||||
var s = ws[id];
|
||||
if (!s || !s.queue.length) return -1;
|
||||
pending = new TextEncoder().encode(s.queue.shift());
|
||||
return pending.length;
|
||||
};
|
||||
env.dc_ws_close = function (id) {
|
||||
var s = ws[id];
|
||||
if (!s) return;
|
||||
try { if (s.sock) s.sock.close(); } catch (e) { }
|
||||
delete ws[id];
|
||||
};
|
||||
env.dc_storage_get = function (kp, kl) {
|
||||
var v = null;
|
||||
try { v = window.localStorage.getItem(str(kp, kl)); } catch (e) { }
|
||||
|
||||
Reference in new issue
Block a user