@@ -23,13 +23,11 @@ app.post('/pusher/auth', function (req, res) {
23
23
const auth = pusher . authenticate ( socketId , channel , presenceData ) ;
24
24
res . send ( auth ) ;
25
25
} ) ;
26
- const port = process . env . PORT || 5000 ;
26
+ let port = Number ( process . env . PORT || 5000 ) ;
27
27
app . listen ( port ) ;
28
28
29
-
30
-
31
-
32
- const io = require ( "socket.io" ) ( 3000 , {
29
+ port ++ ;
30
+ const io = require ( "socket.io" ) ( port , {
33
31
path : "/" ,
34
32
serveClient : true ,
35
33
pingInterval : 10000 ,
@@ -56,8 +54,8 @@ io.on('connection', function (socket) {
56
54
addedUser = true ;
57
55
socket . username = data . username ;
58
56
console . log ( 'Username: ' , data . username ) ;
59
- userList . push ( { username : data . username } ) ;
60
- socket . emit ( 'login' , { userList : userList } ) ;
57
+ userList . push ( { username : data . username } ) ;
58
+ socket . emit ( 'login' , { userList : userList } ) ;
61
59
socket . broadcast . emit ( 'user joined' , {
62
60
username : data . username
63
61
} ) ;
@@ -86,7 +84,7 @@ io.on('connection', function (socket) {
86
84
console . log ( 'User Disconnected' )
87
85
if ( addedUser ) {
88
86
for ( let i = 0 ; i < userList . length ; i ++ ) {
89
- if ( socket . username === userList [ i ] . username ) {
87
+ if ( socket . username === userList [ i ] . username ) {
90
88
userList . splice ( i , 1 )
91
89
}
92
90
}
@@ -113,3 +111,197 @@ nspChat.on('connect', (socket) => {
113
111
} )
114
112
} )
115
113
114
+
115
+ /* WebRTC 1O1 */
116
+
117
+ port ++ ;
118
+ const wio = require ( "socket.io" ) ( port , {
119
+ path : "/" ,
120
+ serveClient : true ,
121
+ pingInterval : 10000 ,
122
+ pingTimeout : 5000 ,
123
+ cookie : false
124
+ } ) ;
125
+
126
+ const wMessageList = [ ]
127
+ let userMap = new Map ( ) ;
128
+ const lobby = 'lobby' ;
129
+
130
+ function getOtherUser ( id ) {
131
+ const keys = Array . from ( userMap . keys ( ) ) ;
132
+ const filtered = keys . filter ( ( value ) => {
133
+ if ( value . indexOf ( id ) === - 1 ) {
134
+ return value ;
135
+ }
136
+ } ) ;
137
+ return userMap . get ( filtered [ 0 ] ) ;
138
+ }
139
+
140
+ wio . on ( 'connection' , function ( socket ) {
141
+ socket . on ( 'disconnect' , function ( ) {
142
+ userMap . delete ( socket . id ) ;
143
+ } ) ;
144
+ userMap . set ( socket . id , "" ) ;
145
+ console . log ( 'User Connected: -> ' + socket . id ) ;
146
+ socket . emit ( 'connected' , 'Welcome' ) ;
147
+
148
+ socket . on ( 'init' , function ( data ) {
149
+ socket . join ( data . id ) ;
150
+ userMap . set ( socket . id , data . id ) ;
151
+ } ) ;
152
+
153
+ socket . on ( 'call' , function ( data ) {
154
+ console . log ( 'call from' , data . from ) ;
155
+ const otherId = getOtherUser ( socket . id ) ;
156
+ io . in ( otherId ) . emit ( 'call:incoming' , {
157
+ from : data . from ,
158
+ to : otherId ,
159
+ sdp : data . sdp
160
+ } ) ;
161
+ } ) ;
162
+
163
+ socket . on ( 'iceCandidate' , function ( data ) {
164
+ console . log ( 'call:iceCandidate' , data . from ) ;
165
+ const otherId = getOtherUser ( socket . id ) ;
166
+ io . in ( otherId ) . emit ( 'call:iceCandidate' , Object . assign ( { } , data , {
167
+ to : otherId
168
+ } ) ) ;
169
+ } ) ;
170
+
171
+ socket . on ( 'answer' , function ( data ) {
172
+ console . log ( 'call:answer' , data . from ) ;
173
+ const otherId = getOtherUser ( socket . id ) ;
174
+ io . in ( otherId ) . emit ( 'call:answer' , {
175
+ from : data . from ,
176
+ sdp : data . sdp ,
177
+ to : otherId
178
+ } ) ;
179
+ } ) ;
180
+
181
+ socket . on ( 'answered' , function ( data ) {
182
+ console . log ( 'call:answered' , data . from ) ;
183
+ const otherId = getOtherUser ( socket . id ) ;
184
+ io . in ( otherId ) . emit ( 'call:answered' , {
185
+ from : data . from ,
186
+ sdp : data . sdp ,
187
+ to : otherId
188
+ } ) ;
189
+ } )
190
+
191
+ } ) ;
192
+
193
+
194
+ port ++ ;
195
+ const wio1 = require ( "socket.io" ) ( port , {
196
+ path : "/" ,
197
+ serveClient : true ,
198
+ pingInterval : 10000 ,
199
+ pingTimeout : 5000 ,
200
+ cookie : false
201
+ } ) ;
202
+
203
+ const wio1NspChat = wio1 . of ( '/chat' )
204
+ const wio1NspDefault = wio1 . of ( '/' )
205
+
206
+ let wio1MessageList = [ ]
207
+ let wio1UserList = [ ]
208
+
209
+ wio1 . on ( 'connection' , function ( socket ) {
210
+ console . log ( 'User Connected' ) ;
211
+ socket . emit ( 'connected' , 'Welcome' ) ;
212
+ let addedUser = false ;
213
+
214
+ socket . on ( 'add user' , function ( data ) {
215
+ if ( addedUser ) return ;
216
+ addedUser = true ;
217
+ socket . username = data . username ;
218
+ console . log ( 'Username: ' , data . username ) ;
219
+ wio1UserList . push ( {
220
+ username : data . username
221
+ } ) ;
222
+
223
+ socket . emit ( 'login' , {
224
+ userList : wio1UserList
225
+ } ) ;
226
+
227
+ socket . broadcast . emit ( 'user joined' , {
228
+ username : data . username
229
+ } ) ;
230
+
231
+ socket . join ( data . username ) ;
232
+ } ) ;
233
+
234
+ socket . on ( 'call' , function ( data ) {
235
+ console . log ( 'call from' , data . from ) ;
236
+ console . log ( 'call to' , data . to ) ;
237
+ io . sockets . in ( data . to ) . emit ( 'call:incoming' , data ) ;
238
+ } ) ;
239
+
240
+
241
+ socket . on ( 'iceCandidate' , function ( data ) {
242
+ io . sockets . in ( data . to ) . emit ( 'call:iceCandidate' , data ) ;
243
+ } ) ;
244
+
245
+ socket . on ( 'answer' , function ( data ) {
246
+ console . log ( 'answer from' , data . from ) ;
247
+ console . log ( 'answer to' , data . to ) ;
248
+ io . sockets . in ( data . to ) . emit ( 'call:answer' , data ) ;
249
+ } ) ;
250
+
251
+ socket . on ( 'answered' , function ( data ) {
252
+ console . log ( 'answer from' , data . from ) ;
253
+ console . log ( 'answer to' , data . to ) ;
254
+ io . sockets . in ( data . to ) . emit ( 'call:answered' , data ) ;
255
+ } )
256
+
257
+ socket . on ( 'new message' , function ( data , cb ) {
258
+ cb ( true ) ;
259
+ console . log ( data ) ;
260
+ wio1MessageList . push ( data ) ;
261
+ socket . broadcast . emit ( 'new message' , data ) ;
262
+ } )
263
+
264
+ socket . on ( 'getUsers' , function ( ) {
265
+ socket . emit ( 'getUsers' , wio1UserList ) ;
266
+ } )
267
+ socket . on ( 'user count' , function ( ) {
268
+ socket . emit ( 'user count' , wio1UserList . length )
269
+ } )
270
+ socket . on ( 'getMessages' , function ( ) {
271
+ socket . emit ( 'getMessages' , wio1MessageList )
272
+ } )
273
+
274
+ socket . on ( 'disconnect' , function ( ) {
275
+ console . log ( 'User Disconnected' ) ;
276
+ if ( addedUser ) {
277
+ for ( let i = 0 ; i < wio1UserList . length ; i ++ ) {
278
+ if ( socket . username === wio1UserList [ i ] . username ) {
279
+ wio1UserList . splice ( i , 1 ) ;
280
+ }
281
+ }
282
+ socket . broadcast . emit ( 'user left' , {
283
+ username : socket . username
284
+ } ) ;
285
+
286
+ socket . emit ( 'getUsers' , wio1UserList ) ;
287
+ }
288
+ } )
289
+ } )
290
+
291
+ wio1NspDefault . on ( 'connect' , ( socket ) => {
292
+ console . log ( 'Joined Namespace: /' )
293
+ socket . on ( 'disconnect' , ( ) => {
294
+ console . log ( 'Left Namespace: /' )
295
+ } )
296
+ } )
297
+
298
+ wio1NspChat . on ( 'connect' , ( socket ) => {
299
+ console . log ( 'Joined Namespace: /chat' )
300
+
301
+ socket . on ( 'disconnect' , ( ) => {
302
+ console . log ( 'Left Namespace: /chat' )
303
+ } )
304
+ } )
305
+
306
+
307
+
0 commit comments