@@ -24,82 +24,90 @@ app.use(bodyParser.urlencoded({ extended: true }))
24
24
app . use ( cors ( ) )
25
25
26
26
app . post ( '/subscribe' , async ( { body } , res ) => {
27
- console . log ( 'got subscriber on IP' , body . ip , 'for topic' , body . topic )
28
-
29
- // Configure the peer connection
30
- const peer = new webrtc . RTCPeerConnection ( {
31
- iceServers : [
32
- {
33
- urls : 'stun:stun1.l.google.com:19302'
34
- }
35
- ]
36
- } )
37
-
38
- // Close any old peers on the same IP address
39
- const topic = body . topic
40
- const key = body . ip + ':' + topic
41
- if ( key in subscribePeers && subscribePeers [ key ] && subscribePeers [ key ] . connectionState !== 'closed' ) {
42
- const senders = subscribePeers [ key ] . getSenders ( )
43
- senders . forEach ( ( sender ) => subscribePeers [ key ] . removeTrack ( sender ) )
44
- subscribePeers [ key ] . close ( )
27
+ try {
28
+ console . log ( 'got subscriber on IP' , body . ip , 'for topic' , body . topic )
29
+
30
+ // Configure the peer connection
31
+ const peer = new webrtc . RTCPeerConnection ( {
32
+ iceServers : [
33
+ {
34
+ urls : 'stun:stun1.l.google.com:19302'
35
+ }
36
+ ]
37
+ } )
38
+
39
+ // Close any old peers on the same IP address
40
+ const topic = body . topic
41
+ const key = body . ip + ':' + topic
42
+ if ( key in subscribePeers && subscribePeers [ key ] && subscribePeers [ key ] . connectionState !== 'closed' ) {
43
+ const senders = subscribePeers [ key ] . getSenders ( )
44
+ senders . forEach ( ( sender ) => subscribePeers [ key ] . removeTrack ( sender ) )
45
+ subscribePeers [ key ] . close ( )
46
+ }
47
+ subscribePeers [ key ] = peer
48
+
49
+ const desc = new webrtc . RTCSessionDescription ( body . sdp )
50
+ await peer . setRemoteDescription ( desc )
51
+
52
+ // Add the publisher's video stream to the subscriber's peer connection
53
+ if ( topic in senderStream ) {
54
+ senderStream [ topic ] . getTracks ( ) . forEach ( ( track ) => peer . addTrack ( track , senderStream [ topic ] ) )
55
+ }
56
+
57
+ // Create an answer to the publisher's offer
58
+ const answer = await peer . createAnswer ( )
59
+ await peer . setLocalDescription ( answer )
60
+ const payload = {
61
+ sdp : peer . localDescription
62
+ }
63
+
64
+ // Send the answer to the publisher
65
+ res . json ( payload )
66
+ } catch ( err ) {
67
+ console . error ( 'Failed to process subscriber, exception: ' + err . message )
45
68
}
46
- subscribePeers [ key ] = peer
47
-
48
- const desc = new webrtc . RTCSessionDescription ( body . sdp )
49
- await peer . setRemoteDescription ( desc )
50
-
51
- // Add the publisher's video stream to the subscriber's peer connection
52
- if ( topic in senderStream ) {
53
- senderStream [ topic ] . getTracks ( ) . forEach ( ( track ) => peer . addTrack ( track , senderStream [ topic ] ) )
54
- }
55
-
56
- // Create an answer to the publisher's offer
57
- const answer = await peer . createAnswer ( )
58
- await peer . setLocalDescription ( answer )
59
- const payload = {
60
- sdp : peer . localDescription
61
- }
62
-
63
- // Send the answer to the publisher
64
- res . json ( payload )
65
69
} )
66
70
67
71
app . post ( '/publish' , async ( { body } , res ) => {
68
- console . log ( 'got publisher on IP' , body . ip , 'for topic' , body . topic )
69
-
70
- // Configure the peer connection
71
- const peer = new webrtc . RTCPeerConnection ( {
72
- iceServers : [
73
- {
74
- urls : 'stun:stun1.l.google.com:19302'
75
- }
76
- ]
77
- } )
78
-
79
- // Close any old peers on the same IP address
80
- const topic = body . topic
81
- const key = body . ip + ':' + topic
82
- if ( key in publishPeers && publishPeers [ key ] && publishPeers [ key ] . connectionState !== 'closed' ) {
83
- const senders = publishPeers [ key ] . getSenders ( )
84
- senders . forEach ( ( sender ) => publishPeers [ key ] . removeTrack ( sender ) )
85
- publishPeers [ key ] . close ( )
72
+ try {
73
+ console . log ( 'got publisher on IP' , body . ip , 'for topic' , body . topic )
74
+
75
+ // Configure the peer connection
76
+ const peer = new webrtc . RTCPeerConnection ( {
77
+ iceServers : [
78
+ {
79
+ urls : 'stun:stun1.l.google.com:19302'
80
+ }
81
+ ]
82
+ } )
83
+
84
+ // Close any old peers on the same IP address
85
+ const topic = body . topic
86
+ const key = body . ip + ':' + topic
87
+ if ( key in publishPeers && publishPeers [ key ] && publishPeers [ key ] . connectionState !== 'closed' ) {
88
+ const senders = publishPeers [ key ] . getSenders ( )
89
+ senders . forEach ( ( sender ) => publishPeers [ key ] . removeTrack ( sender ) )
90
+ publishPeers [ key ] . close ( )
91
+ }
92
+ publishPeers [ key ] = peer
93
+
94
+ // Send the publisher's video stream to all subscribers on that topic
95
+ peer . ontrack = ( e ) => handleTrackEvent ( e , topic )
96
+
97
+ // Create an answer to the publisher's offer
98
+ const desc = new webrtc . RTCSessionDescription ( body . sdp )
99
+ await peer . setRemoteDescription ( desc )
100
+ const answer = await peer . createAnswer ( )
101
+ await peer . setLocalDescription ( answer )
102
+ const payload = {
103
+ sdp : peer . localDescription
104
+ }
105
+
106
+ // Send the answer to the publisher
107
+ res . json ( payload )
108
+ } catch ( err ) {
109
+ console . error ( 'Failed to process publisher, exception: ' + err . message )
86
110
}
87
- publishPeers [ key ] = peer
88
-
89
- // Send the publisher's video stream to all subscribers on that topic
90
- peer . ontrack = ( e ) => handleTrackEvent ( e , topic )
91
-
92
- // Create an answer to the publisher's offer
93
- const desc = new webrtc . RTCSessionDescription ( body . sdp )
94
- await peer . setRemoteDescription ( desc )
95
- const answer = await peer . createAnswer ( )
96
- await peer . setLocalDescription ( answer )
97
- const payload = {
98
- sdp : peer . localDescription
99
- }
100
-
101
- // Send the answer to the publisher
102
- res . json ( payload )
103
111
} )
104
112
105
113
function handleTrackEvent ( e , topic ) {
0 commit comments