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
|
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(function(){
Math.floor(Math.random() * 16).toString(16);
}).join('');
}
window.PEER = {
event_queue: [],
peers: {},
message_queue: [],
connections: {},
create: function(tbl) {
var name = tbl.name;
var options = tbl.options;
console.log("[JS] Creating peer " + name);
var peer = new Peer(name, options);
PEER.peers[name] = peer;
},
on: function(tbl) {
var name = tbl.name;
var e = tbl.e;
var message = tbl.message;
console.log("[JS] Setting hook for " + name + "," + e + "," + message);
PEER.peers[name].on(e, function(data) {
console.log("[JS] Peer " + name + " received " + e);
if(e == "connection"){
PEER.connections[[name,data.peer]] = data;
data = [name,data.peer]; // rewrite connections
}
PEER.message_queue.push({"message":message, "data":{
"call": "on",
"peer": name,
"e": e,
"data": data
}});
});
},
connect: function(tbl) {
var name = tbl.name;
var id = tbl.id;
console.log("[JS] connecting " + name + " to " + id);
var conn = PEER.peers[name].connect(id);
PEER.connections[[name,id]] = conn;
return [name,id];
},
disconnect: function(tbl) {
PEER.peers[tbl.name].disconnect();
},
reconnect: function(tbl){
PEER.peers[tbl.name].reconnect();
},
destroy: function(tbl){
PEER.peers[tbl.name].destroy();
},
send: function(tbl){
var name = tbl.name;
var id = tbl.id;
var data = tbl.data;
console.log("[JS] " + name + " is sending " + data + " to " + id);
console.log(PEER.connections[[name,id]]);
console.log(data);
PEER.connections[[name,id]].send(data);
},
close: function(tbl){
var name = tbl.name;
var id = tbl.id;
PEER.connections[[name,id]].close();
},
conn_on: function(tbl){
var name = tbl.name;
var id = tbl.id;
var e = tbl.e;
var message = tbl.message;
console.log("[JS] Setting hook for [" + name + "," + id + "] " + e + "," + message);
PEER.connections[[name,id]].on(e, function(c) {
console.log("[JS] connection between " + name + " and " + id + " received " + e);
PEER.message_queue.push({"message":message, "data":{
"call": "on",
"peer": name,
"id": id,
"e": e,
"data": c
}});
});
},
};
|