diff options
| author | Alexander M Pickering <alex@cogarr.net> | 2024-01-29 16:20:10 -0600 |
|---|---|---|
| committer | Alexander M Pickering <alex@cogarr.net> | 2024-01-29 16:20:10 -0600 |
| commit | c2926c5ec74d7e37da395c8c32a7ff2b4cae7d06 (patch) | |
| tree | ac2bb208dab1274cdc5e9059ffe014ae19181a4c /src/js/joined.js | |
| download | fools_rush_in-c2926c5ec74d7e37da395c8c32a7ff2b4cae7d06.tar.gz fools_rush_in-c2926c5ec74d7e37da395c8c32a7ff2b4cae7d06.tar.bz2 fools_rush_in-c2926c5ec74d7e37da395c8c32a7ff2b4cae7d06.zip | |
All the files
Diffstat (limited to 'src/js/joined.js')
| -rw-r--r-- | src/js/joined.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/js/joined.js b/src/js/joined.js new file mode 100644 index 0000000..0011625 --- /dev/null +++ b/src/js/joined.js @@ -0,0 +1,48 @@ +var s = document.createElement('script'); +s.setAttribute('src','https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js'); +document.body.appendChild(s); +CLIENT = {}; +CLIENT["message_queue"] = []; +CLIENT["open"] = false; +CLIENT.lobby_id = null; +CLIENT["join"] = function(id) { + if(CLIENT.lobby_id != null){ + console.log("Somehow called .join() twice") + return; + } + CLIENT.lobby_id = id; + console.log("lobby_id:" + CLIENT.lobby_id); + var peer = new Peer(); + peer.on("open",function(){ + CLIENT.peer = peer; + var conn = CLIENT.peer.connect("ANGRY_ADVENTURE_" + CLIENT.lobby_id); + CLIENT.conn = conn + console.log("conn is:"); + console.log(conn); + conn.on("open",function(){ + console.log("Opened peer"); + CLIENT.open = true + conn.on("data",function(data) { + console.log("Got data:" + data); + CLIENT.message_queue.push(data); + }) + }); + + }) + peer.on('error',function(err){ + console.log("Error on peer:"); + console.log(err); + CLIENT.error = err; + }); +}; +CLIENT.send = function(data) { + CLIENT.conn.send(data); +}; +CLIENT.get = function(){ + if(CLIENT.message_queue.length >= 1){ + return CLIENT.message_queue.shift(); + }else{ + return null; + } +} +true; |
