-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
99 lines (85 loc) · 2.89 KB
/
server.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
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
const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({port: 8000}, () => {
console.log('server is running')
});
let users = [],
rooms = [],
clients = []
const sendStatus = (connection, status) => {
let message = {status}
connection.send(JSON.stringify(message))
}
const addUsername = (connection, username) => {
if (!users.includes(username)) {
users.push(username)
sendStatus(connection, 'success')
const foundClient = clients.find(client => client.connection === connection)
foundClient.username = username
} else {
sendStatus(connection, 'fail')
}
}
const addRoom = (connection, roomname) => {
if (!rooms.includes(roomname)) {
rooms.push({roomname, offer: null})
sendStatus(connection, 'success')
let foundClient = clients.find(client => client.connection === connection)
foundClient.roomname = roomname
} else {
sendStatus(connection, 'fail')
}
}
const getRoom = (connection) => {
connection.send(JSON.stringify(rooms.map(room => room.roomname)))
}
const handleOffer = (messageObj) => {
const selectRoom = rooms.find(room => room.roomname === messageObj.roomname)
selectRoom.offer = messageObj.offer
}
const getOffer = (messageObj, connection) => {
const selectRoom = rooms.find(room => room.roomname === messageObj.roomname)
connection.send(JSON.stringify(selectRoom.offer))
let foundClient = clients.find(client => client.connection === connection)
foundClient.roomname = messageObj.roomname
clients.filter(client => client.roomname === messageObj.roomname).forEach(cl => cl.connection.send(JSON.stringify({type: 'join', username: foundClient.username})))
}
const getAnswer = (messageObj, connection) => {
connection.send(JSON.stringify(messageObj.answer))
}
const getCandidate = (messageObj, connection) => {
const selectRoom = rooms.find(room => room.roomname === messageObj.roomname)
selectRoom.candidate = messageObj.candidate
clients.map(client => client.connection).forEach(connection => {
connection.send(JSON.stringify(messageObj))
})
}
wss.on('connection', connection => {
clients.push({connection, username: null, roomname: null})
connection.on('message', (message) => {
let messageObj = JSON.parse(message)
switch (messageObj.type) {
case 'addUsername':
addUsername(connection, messageObj.username)
break
case 'addRoomname':
addRoom(connection, messageObj.roomname)
break
case 'getRoomname':
getRoom(connection)
break
case 'offer':
handleOffer(messageObj)
break
case 'getOffer':
getOffer(messageObj, connection)
break
case 'answer':
getAnswer(messageObj, connection)
case 'candidate':
getCandidate(messageObj, connection)
}
})
})
setInterval(() => {
clients = clients.filter(client => client.connection.readyState === client.connection.OPEN)
}, 100)