Repository navigation
Expand file tree
/
Copy pathrtc_client.js
More file actions
154 lines (130 loc) · 4.83 KB
/
Copy pathrtc_client.js
File metadata and controls
154 lines (130 loc) · 4.83 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Make a socket io connection
function Client(url) {
this.socket = io.connect(url);
this.id = localStorage.getItem("rtc_id");
this.localStream = null;
var self = this;
this.socket.on("control_call", function (msg) {
console.log("Received message " + JSON.stringify(msg));
var pc = self.connections[msg.from];
if (pc.waiting) {
var npc = createPeerConnection(self, pc.caller_socket);
npc.caller = pc.caller;
npc.caller_socket = pc.caller_socket;
npc.processSignalingMessage(msg.data);
npc.addStream(self.localStream);
self.connections[msg.from] = npc;
}
else if (pc) pc.processSignalingMessage(msg.data);
});
this.socket.on("call", function (msg, cont) {
if (self.localStream && self.oncall) self.oncall(msg, function () { self.acceptCall(msg, cont); }, function () { self.rejectCall(msg, cont); });
});
this.socket.on("hangup_call", function (msg, cont) {
if (self.onhangup) self.onhangup(msg, cont);
});
this.socket.on("message", function (msg, cont) {
if (self.onmessage) self.onmessage(msg);
});
this.connections = {};
}
exports.Client = Client;
function createPeerConnection(client, id) {
try {
var pc = new webkitPeerConnection("STUN stun.l.google.com:19302", function (e) { client.signal(id, e); });
pc.onaddstream = function (e) { client.addStream(id, e); };
pc.onremovestream = function (e) { client.removeStream(id, e); };
pc.onconnecting = function (e) { console.log("Connecting " + id); };
pc.onopen = function (e) { console.log("Opening " + id); };
pc.onstatechange = function (e) { console.log("State change: " + id); };
return pc;
}
catch (e) {
console.log(e);
}
}
Client.prototype.sendMessage = function (id, str, cont) {
this.socket.emit("message", {id:id, message: str});
};
Client.prototype.call = function (id, cont) {
// Cannot create peerconnection before knows the name of target socket
var self = this;
this.socket.emit("call", {id:id}, function (obj) {
console.log("Making call");
console.log(obj);
if (!obj.error) {
var pc = {waiting: true, caller: obj.user, caller_socket: obj.socket};
self.connections[obj.socket] = pc;
/*
createPeerConnection(self, obj.socket);
pc.addStream(self.localStream);
self.connections[obj.socket] = pc;
pc.caller = id;
pc.caller_socket = obj.socket;
*/
}
cont(obj);
});
};
Client.prototype.acceptCall = function (msg, cont) {
var pc = createPeerConnection(this, msg.caller_socket);
// pc.processSignalingMessage(msg.data);
this.connections[msg.caller_socket] = pc;
pc.caller = msg.caller;
pc.caller_socket = msg.caller_socket;
pc.addStream(this.localStream);
cont({msg:"Accepted"});
};
Client.prototype.rejectCall = function (msg, cont) {
// cont({msg:"Rejected"});
};
Client.prototype.iterConnections = function (f) {
for (var i in this.connections) f(i, this.connections[i]);
};
Client.prototype.signal = function (id, e) {
console.log("Signalling data");
this.socket.emit("control_call", {to:id, data:e});
};
Client.prototype.addStream = function (id, e) {
if (this.onaddstream) this.onaddstream(id, webkitURL.createObjectURL(e.stream));
};
Client.prototype.removeStream = function (id, e) {
if (this.onremovestream) this.onremovestream(id, e);
};
// Store user id into localStorage
Client.prototype.register = function (id, nick, cont) {
this.id = id;
this.socket.emit("register", {id:id, nick:nick}, function (obj) {
if (!obj.error) localStorage.setItem("rtc_id", id);
cont(obj);
});
};
Client.prototype.connect = function (cont) {
if (!this.id) cont({error:"No identity"});
else this.socket.emit("connect_user", {id:this.id}, cont);
};
Client.prototype.disconnect = function () {
this.socket.emit("unregister", {});
// this.socket.disconnect();
};
Client.prototype.search = function (str, cont) {
this.socket.emit("list", {search_string:str}, cont);
};
Client.prototype.hangup = function (id, cont) {
var pc = this.connections[id];
if (!pc) {
cont({error:"Unknown call"});
return;
}
pc.close();
this.socket.emit("hangup_call", {to:id}, cont);
};
/*
var cl = new last.Client("http://192.168.0.7:8889");
cl.oncall = function (msg, accept) { accept(); };
cl.register("mrsmkl@gmail.com", "Sami_ZzZ", function (x) { console.log(x); });
cl.register("sanna.tuohimaa@gmail.com", "Sanna_ZzZ", function (x) { console.log(x); });
cl.connect(function (x) { console.log(x); });
cl.search(".*", function (x) { console.log(x); });
cl.call("sanna.tuohimaa@gmail.com", function (x) { console.log(x); });
*/