forked from xiaoyanhao/chatroulette
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvue.config.js
More file actions
125 lines (104 loc) · 2.82 KB
/
vue.config.js
File metadata and controls
125 lines (104 loc) · 2.82 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const WebSocket = require('ws')
const crypto = require('crypto')
const port = 8081
const wsServer = new WebSocket.Server({
port,
clientTracking: true
})
const idLen = 8
let waitingQueue = []
let matchedIds = new Map()
function log (text) {
const time = new Date()
console.log('[' + time.toLocaleString() + '] ' + text)
}
function getPeerSocket (peerId) {
for (let client of wsServer.clients) {
if (client.id === peerId && client.readyState === WebSocket.OPEN) {
return client
}
}
return null
}
function searchPeer (socket, msg) {
while (waitingQueue.length) {
let index = Math.floor(Math.random() * waitingQueue.length)
let peerId = waitingQueue[index]
let peerSocket = getPeerSocket(peerId)
waitingQueue.splice(index, 1)
if (peerSocket) {
matchedIds.set(socket.id, peerId)
matchedIds.set(peerId, socket.id)
socket.send(JSON.stringify(msg))
log(`#${socket.id} matches #${peerId}`)
return
}
}
waitingQueue.push(socket.id)
log(`#${socket.id} adds self into waiting queue`)
}
function hangUp (socketId, msg) {
if (matchedIds.has(socketId)) {
let peerId = matchedIds.get(socketId)
let peerSocket = getPeerSocket(peerId)
matchedIds.delete(socketId)
matchedIds.delete(peerId)
if (peerSocket) {
peerSocket.send(JSON.stringify(msg))
log(`#${socketId} hangs up #${peerId}`)
return
}
} else {
let myIndex = waitingQueue.indexOf(socketId)
if (myIndex !== -1) {
waitingQueue.splice(myIndex, 1)
log(`#${socketId} removes self from waiting queue`)
}
}
}
function sendToPeer (socketId, msg) {
if (!matchedIds.has(socketId)) {
return
}
let peerId = matchedIds.get(socketId)
let peerSocket = getPeerSocket(peerId)
if (peerSocket) {
peerSocket.send(JSON.stringify({ type: msg.type, data: msg.data }))
log(`#${socketId} sends ${msg.type} to #${peerId}`)
return
}
}
wsServer.on('connection', function (socket) {
socket.id = crypto.randomBytes(idLen / 2).toString('hex').slice(0, idLen)
log(`#${socket.id} connected`)
socket.on('message', (message) => {
let msg = JSON.parse(message)
switch (msg.type) {
case 'new-ice-candidate':
case 'video-offer':
case 'video-answer':
case 'message':
case 'canvas':
sendToPeer(socket.id, msg)
break
case 'hang-up':
hangUp(socket.id, { type: 'hang-up' })
break
case 'search-peer':
searchPeer(socket, { type: 'peer-matched' })
break
case 'ping':
socket.send(JSON.stringify({ type: 'pong' }))
break
default:
break
}
})
socket.on('close', (code, reason) => {
log(`#${socket.id} disconnected: [${code}]${reason}`)
hangUp(socket.id, { type: 'hang-up' })
})
})
module.exports = {
productionSourceMap: false
}