forked from iknownothing13/RTC_
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (58 loc) · 1.98 KB
/
app.js
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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
app.use(express.static('public'));
http.listen(port, () => console.log('app started'))
io.on('connection', socket => {
console.log('a user connected with id:'+socket.id);
setTimeout(function(){
socket.emit('sending_socket_id',{description:socket.id})
},1)
var name;
socket.on('sending_name_to_server',function(data){
console.log("socket id: "+socket.id+" name: "+(data.description));
name=data.description;
socket.broadcast.emit('new_client_name',{description:name})
})
socket.on('sending_message',function(data){
socket.broadcast.emit('broadcasting_sent_message',{description:data.description})
})
socket.on('asking_download',function(data){
socket.emit
})
//when anyone disconnects:
socket.on('disconnect',function(){
console.log('a user has disconnected:'+socket.id)
})
console.log('user connected');
socket.on('create or join', room => {
console.log('create or join room', room)
room = room.trim();
const myRoom = io.sockets.adapter.rooms.get(room) || { size: 0 };
const numClients = myRoom.size;
console.log(room, 'has', numClients, 'clients');
if (numClients === 0) {
socket.join(room);
socket.emit(`created`, room);
} else if (numClients === 1) {
socket.join(room);
socket.emit(`joined`, room);
} else {
socket.emit(`full`, room);
}
});
socket.on('ready', room => {
socket.broadcast.to(room).emit('ready');
})
socket.on('candidate', event => {
socket.broadcast.to(event.room).emit('candidate', event);
})
socket.on('offer', event => {
socket.broadcast.to(event.room).emit('offer', event.sdp);
})
socket.on('answer', event => {
socket.broadcast.to(event.room).emit('answer', event.sdp);
})
});