This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
111 lines (100 loc) · 3.06 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
100
101
102
103
104
105
106
107
108
109
110
111
'use strict'
var os = require('os')
var nodeStatic = require('node-static')
var http = require('http')
var socketIO = require('socket.io')
var hashwords = require('hashwords')
const hasher = hashwords({salt: '~webtendo~'})
function getRoomId (seed) {
return hasher.hashStr(seed).toLowerCase().split(' ').slice(0, 2).join('-')
}
var file = new nodeStatic.Server('./public')
var app = http.createServer(function (req, res) {
file.serve(req, res)
}).listen(process.env.PORT || 8080)
var hosts = {}
var clients = {}
var io = socketIO.listen(app)
io.sockets.on('connection', function (socket) {
var socketClientId
var hostId
var clientIP
var socketRoom
if (socket.request.headers['x-forwarded-for']) {
clientIP = socket.request.headers['x-forwarded-for']
console.log('clientIP', clientIP, 'aka', getRoomId(clientIP))
}
// convenience function to log server messages on the client
function log () {
var array = ['Message from server:']
array.push.apply(array, arguments)
// socket.emit('log', array);
console.log(array)
}
// Message passthrough for signaling.
socket.on('message', message => {
log('Client said: ', message)
if (message.recipient) {
io.to(clients[message.recipient]).emit('message', message)
} else {
io.to(socketRoom).emit('message', message)
}
})
socket.on('get room', () => {
socket.emit('room', getRoomId(clientIP))
})
socket.on('create or join', function (room, clientId, isHost) {
socketRoom = room || getRoomId(clientIP)
room = socketRoom
log('create or join room ' + room + ' from ' + clientId)
clients[clientId] = socket.id
socketClientId = clientId
if (isHost) {
socket.join(room)
hosts[room] = clientId
io.to(room).emit('created', room, clientId)
return
}
let numClients
if (!io.sockets.adapter.rooms[room]) {
socket.emit('nohost', room)
numClients = 0
} else {
numClients = io.sockets.adapter.rooms[room].length
}
log('Room ' + room + ' now has ' + numClients + ' client(s)')
if (numClients < 30) {
log('Client ID ' + socket.id + ' joined room ' + room)
socket.join(room)
hostId = hosts[room]
if (hostId) {
// Tell host to send an offer.
io.to(clients[hostId]).emit('joined', room, clientId)
}
// Echo back to sender so they know they joined successfully.
socket.emit('joined', room, clientId)
} else { // max clients
socket.emit('full', room)
}
})
socket.on('disconnect', () => {
if (!socketClientId) {
return
}
console.log(socketClientId, 'disconnected from ', socketRoom)
io.to(clients[hostId]).emit('disconnected', socketClientId)
})
socket.on('ipaddr', function () {
var ifaces = os.networkInterfaces()
for (var dev in ifaces) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
socket.emit('ipaddr', details.address)
}
})
}
})
socket.on('bye', function () {
console.log('received bye')
})
})