Skip to content

Commit 1252e00

Browse files
author
cloudwebrtc
committed
Remove ringing|invite cmd of signaling.
1 parent b48497d commit 1252e00

File tree

3 files changed

+48
-110
lines changed

3 files changed

+48
-110
lines changed

server/Server.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export default class CallHandler {
163163
});
164164
}
165165
break;
166-
case "invite":
166+
case "offer":
167167
{
168168
var peer = null;
169169
this.clients.forEach(function (client) {
@@ -173,27 +173,21 @@ export default class CallHandler {
173173
});
174174

175175
if (peer != null) {
176-
var msg = {
177-
type: "ringing",
178-
data: {
179-
id: peer.id,
180-
media: message.media,
181-
}
182-
};
183-
client_self.send(JSON.stringify(msg));
184-
client_self.session_id = message.session_id;
185176

186177
msg = {
187-
type: "invite",
178+
type: "offer",
188179
data: {
189180
to: peer.id,
190181
from: client_self.id,
191182
media: message.media,
192183
session_id: message.session_id,
184+
description: message.description,
193185
}
194186
}
195187
peer.send(JSON.stringify(msg));
188+
196189
peer.session_id = message.session_id;
190+
client_self.session_id = message.session_id;
197191

198192
let session = {
199193
id: message.session_id,
@@ -205,28 +199,6 @@ export default class CallHandler {
205199

206200
break;
207201
}
208-
case 'offer':
209-
{
210-
var msg = {
211-
type: "offer",
212-
data: {
213-
from: client_self.id,
214-
to: message.to,
215-
description: message.description,
216-
},
217-
};
218-
219-
this.clients.forEach(function (client) {
220-
if (client.id === "" + message.to && client.session_id === message.session_id) {
221-
try {
222-
client.send(JSON.stringify(msg));
223-
} catch (e) {
224-
console.log("onUserJoin:" + e.message);
225-
}
226-
}
227-
});
228-
}
229-
break;
230202
case 'answer':
231203
{
232204
var msg = {

src/App.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ class App extends Component {
8282
this.setState({peers, self_id: self});
8383
});
8484

85-
this.signaling.on('ringing',(id) => {
86-
this.setState({ open:true });
87-
});
88-
89-
this.signaling.on('invite',(from, sessios) => {
85+
this.signaling.on('new_call',(from, sessios) => {
9086
this.setState({ open:true });
9187
});
9288

@@ -102,7 +98,7 @@ class App extends Component {
10298
this.setState({remoteStream: null});
10399
});
104100

105-
this.signaling.on('bye',(to, session) => {
101+
this.signaling.on('call_end',(to, session) => {
106102
this.setState({ open:false, localStream: null, remoteStream: null });
107103
});
108104

@@ -120,7 +116,7 @@ class App extends Component {
120116
};
121117

122118
handleInvitePeer = (peer_id, type) => {
123-
this.signaling.invitePeer(peer_id, type);
119+
this.signaling.invite(peer_id, type);
124120
}
125121

126122
handleBye = () => {

src/Signaling.js

Lines changed: 40 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,14 @@ export default class Signaling extends events.EventEmitter {
123123
this.socket.send(JSON.stringify(data));
124124
}
125125

126-
invite = (peer_id, type) => {
126+
invite = (peer_id, media) => {
127127
this.session_id = this.self_id + '-' + peer_id;
128-
let message = {
129-
type: 'invite',
130-
session_id: this.session_id,
131-
to: peer_id,
132-
media: type,
133-
}
134-
this.send(message);
128+
this.getLocalStream(media).then((stream) => {
129+
this.local_stream = stream;
130+
this.createPeerConnection(peer_id, media, true, stream);
131+
this.emit('localstream', stream);
132+
this.emit('new_call', this.self_id, this.session_id);
133+
});
135134
}
136135

137136
bye = () => {
@@ -143,41 +142,15 @@ export default class Signaling extends events.EventEmitter {
143142
this.send(message);
144143
}
145144

146-
onRinging = (message) => {
147-
var data = message.data;
148-
var id = data.id;
149-
var media = data.media;
150-
console.log("Remote peer ready: id " + id);
151-
this.emit('ringing', id);
152-
this.getLocalStream(media).then((stream) => {
153-
this.local_stream = stream;
154-
this.createPeerConnection(id, true, stream);
155-
this.emit('localstream', stream);
156-
});
157-
}
158-
159-
onInvite = (message) => {
160-
var data = message.data;
161-
var from = data.from;
162-
console.log("data:" + data);
163-
var media = data.media;
164-
this.session_id = data.session_id;
165-
this.emit('invite', from, this.session_id);
166-
this.getLocalStream(media).then((stream) => {
167-
this.local_stream = stream;
168-
this.emit('localstream', stream);
169-
var pc = this.createPeerConnection(from, false, stream);
170-
});
171-
}
172-
173-
createOffer = (pc, id) => {
145+
createOffer = (pc, id, media) => {
174146
pc.createOffer((desc) => {
175147
console.log('createOffer: ', desc.sdp);
176148
pc.setLocalDescription(desc, () => {
177149
console.log('setLocalDescription', pc.localDescription);
178150
let message = {
179151
type: 'offer',
180152
to: id,
153+
media: media,
181154
description: pc.localDescription,
182155
session_id: this.session_id,
183156
}
@@ -186,7 +159,7 @@ export default class Signaling extends events.EventEmitter {
186159
}, this.logError);
187160
}
188161

189-
createPeerConnection = (id, isOffer, localstream) => {
162+
createPeerConnection = (id, media, isOffer, localstream) => {
190163
var pc = new RTCPeerConnection(configuration);
191164
this.peer_connections["" + id] = pc;
192165
pc.onicecandidate = (event) => {
@@ -229,7 +202,7 @@ export default class Signaling extends events.EventEmitter {
229202
pc.addStream(localstream);
230203

231204
if (isOffer)
232-
this.createOffer(pc, id);
205+
this.createOffer(pc, id, media);
233206
return pc;
234207
}
235208

@@ -269,33 +242,36 @@ export default class Signaling extends events.EventEmitter {
269242
onOffer = (message) => {
270243
var data = message.data;
271244
var from = data.from;
245+
console.log("data:" + data);
246+
var media = data.media;
247+
this.session_id = data.session_id;
248+
this.emit('new_call', from, this.session_id);
272249

273-
console.log("data.from:" + data.from);
274-
275-
var pc = null;
276-
if (from in this.peer_connections) {
277-
pc = this.peer_connections[from];
278-
}
279-
if (pc && data.description) {
280-
//console.log('on offer sdp', data);
281-
pc.setRemoteDescription(new RTCSessionDescription(data.description), () => {
282-
if (pc.remoteDescription.type == "offer")
283-
pc.createAnswer((desc) => {
284-
console.log('createAnswer: ', desc.description);
285-
pc.setLocalDescription(desc, () => {
286-
console.log('setLocalDescription', pc.localDescription);
287-
let message = {
288-
type: 'answer',
289-
to: from,
290-
description: pc.localDescription,
291-
session_id: this.session_id,
292-
}
293-
this.send(message);
250+
this.getLocalStream(media).then((stream) => {
251+
this.local_stream = stream;
252+
this.emit('localstream', stream);
253+
var pc = this.createPeerConnection(from, media, false, stream);
254+
255+
if (pc && data.description) {
256+
//console.log('on offer sdp', data);
257+
pc.setRemoteDescription(new RTCSessionDescription(data.description), () => {
258+
if (pc.remoteDescription.type == "offer")
259+
pc.createAnswer((desc) => {
260+
console.log('createAnswer: ', desc);
261+
pc.setLocalDescription(desc, () => {
262+
console.log('setLocalDescription', pc.localDescription);
263+
let message = {
264+
type: 'answer',
265+
to: from,
266+
description: pc.localDescription,
267+
session_id: this.session_id,
268+
}
269+
this.send(message);
270+
}, this.logError);
294271
}, this.logError);
295-
}, this.logError);
296-
}, this.logError);
297-
}
298-
272+
}, this.logError);
273+
}
274+
});
299275
}
300276

301277
onAnswer = (message) => {
@@ -305,7 +281,6 @@ export default class Signaling extends events.EventEmitter {
305281
if (from in this.peer_connections) {
306282
pc = this.peer_connections[from];
307283
}
308-
309284
if (pc && data.description) {
310285
//console.log('on answer sdp', data);
311286
pc.setRemoteDescription(new RTCSessionDescription(data.description), () => {
@@ -351,7 +326,7 @@ export default class Signaling extends events.EventEmitter {
351326
if (pc !== undefined) {
352327
pc.close();
353328
delete peerConnections[to];
354-
this.emit('bye', to, this.session_id);
329+
this.emit('call_end', to, this.session_id);
355330
}
356331
if (this.local_stream != null) {
357332
this.closeMediaStream(this.local_stream);
@@ -364,11 +339,6 @@ export default class Signaling extends events.EventEmitter {
364339
console.log("logError", error);
365340
}
366341

367-
invitePeer = (peer_id, type) => {
368-
this.invite(peer_id, type);
369-
this.getLocalStream(type);
370-
}
371-
372342
sendText() {
373343
var text = "test send text...";//document.getElementById('textRoomInput').value;
374344
if (text == "") {

0 commit comments

Comments
 (0)