aboutsummaryrefslogtreecommitdiff
path: root/src/js/joined.js
blob: 0011625280f1edcd3687b05654704a30245b4de7 (plain)
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
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;