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
|
var s = document.createElement('script');
s.setAttribute('src','https://unpkg.com/peerjs@1.5.2/dist/peerjs.min.js');
document.body.appendChild(s);
function genRanHex(size) {
return Array.apply(null,Array(size)).map(() => Math.floor(Math.random() * 16).toString(16)).join('');
}
PEER = {}
PEER.event_queue = {}
PEER.peers = {}
PEER.message_queue = {}
PEER["create"] = function(name, options) {
var peer = new Peer(name, options)
PEER.peers[name] = peer
}
PEER["on"] = function(name, e, message) {
PEER.peers[name].on(e, function(conn) {
PEER.message_queue.push(message)
}
}
PEER["connect"] = function(name, id, options) {
PEER.peers[name].connect(id, options)
}
var peer = new Peer("ANGRY_ADVENTURE_" + GLOBAL.lobby_id, {"debug": 3});
var peer_id = null;
var poo = peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
peer.on("connection",function(conn){
console.log("Got a connection!")
GLOBAL.connections[conn.peer] = conn
conn.send("Hello!")
GLOBAL.message_queue.push({
"msg": "data",
"peer": conn.peer,
"data": '{"msg":"player_joined"}'
})
conn.on("data",function(data){
console.log("Got some data from a peer!:" + data)
GLOBAL.message_queue.push({
"msg": "data",
"peer": conn.peer,
"data": data
})
})
conn.on("error",function(err){
console.log("Error on a connection:" + err)
GLOBAL.message_queue.push({
"msg": "error",
"peer": conn.peer,
"err": err
})
})
})
GLOBAL.peer_id = id;
});
|