-
Notifications
You must be signed in to change notification settings - Fork 21
/
serverXHRSignalingChannel.js
131 lines (121 loc) · 4.15 KB
/
serverXHRSignalingChannel.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var log = require('./log').log
var connections = {}, // 链接列表
partner = {}, // 用于在对等端进行映射
messagesFor = {}; // 包含要发送给客户的消息的数组
// 排队发送json响应
function webrtcResponse(response, res) {
log('Replying with webrtc response ' + JSON.stringify(response))
res.writeHead(200, { 'Content-Type': 'application/json' })
res.write(JSON.stringify(response))
res.end()
}
// webrtc发送error响应
function webrtcError(err, res) {
log('Replying with webrtc error ' + err)
webrtcResponse({ 'err': err }, res)
}
// 处理XHR http请求,以使用给定密钥进行连接
function connect(info) {
var res = info.res,
query = info.query,
thisConnection,
newId = function () {
return Math.floor(Math.random() * 1000000000)
},
connectFirstParty = function () {
if (thisConnection.status === 'connected') {
// 删除配对和任何存储的信息
delete partner[thisConnection.ids[0]]
delete partner[thisConnection.ids[1]]
delete messagesFor[thisConnection.ids[0]]
delete messagesFor[thisConnection.ids[1]]
}
connections[query.key] = {}
thisConnection = connections[query.key]
thisConnection.status = 'waiting'
thisConnection.ids = [newId()]
webrtcResponse({
id: thisConnection.ids[0],
status: thisConnection.status
}, res)
},
connectSecondParty = function () {
thisConnection.ids[1] = newId()
partner[thisConnection.ids[0]] = thisConnection.ids[1]
partner[thisConnection.ids[1]] = thisConnection.ids[0]
messagesFor[thisConnection.ids[0]] = []
messagesFor[thisConnection.ids[1]] = []
thisConnection.status = 'connected'
webrtcResponse({
id: thisConnection.ids[1],
status: thisConnection.status
}, res)
}
log('Request handler connect was called.')
if (query && query.key) {
var thisConnection = connections[query.key] || { 'status': 'new' }
if (thisConnection.status === 'waiting') { // 前半部分就绪
connectSecondParty()
return
} else { // 必须为新连接或为‘connected’状态
connectFirstParty()
return
}
} else {
webrtcError('No recognizable query key', res)
}
}
exports.connect = connect
// 对info.postData.message中的消息进行排队处理,已发送至具体info.postData.id中的id的伙伴
function sendMessage(info) {
log('PostData received is *** ' + info.postData + ' ***')
var postData = JSON.parse(info.postData),
res = info.res;
if (typeof postData === 'undefined') {
webrtcError('No posted data in JSON format!', res)
return
}
if (typeof (postData.message) === 'undefined') {
webrtcError('No message received!', res)
return
}
if (typeof (postData.id) === 'undefined') {
webrtcError('No id received with message!', res)
return
}
if (typeof (partner[postData.id]) === 'undefined') {
webrtcError('Invalid id ' + postData.id, res)
return
}
if (typeof (messagesFor[partner[postData.id]]) === 'undefined') {
webrtcError('Invalid id ' + postData.id, res)
return
}
messagesFor[partner[postData.id]].push(postData.message)
log('Saving message *** ' + postData.message + ' *** for delivery to id ' + partner[postData.id])
webrtcResponse('Saving message *** ' + postData.message + ' *** for delivery to id ' + partner[postData.id], res)
}
exports.send = sendMessage
// 返回所有队列获取info.postData.id的消息
function getMessages(info) {
var postData = JSON.parse(info.postData),
res = info.res;
if (typeof postData === 'undefined') {
webrtcError('No posted data in JSON format!', res)
return
}
if (typeof (postData.id) === 'undefined') {
webrtcError('No id received with message!', res)
return
}
if (typeof (messagesFor[partner[postData.id]]) === 'undefined') {
webrtcError('Invalid id ' + postData.id, res)
return
}
log('Sending messages *** ' + JSON.stringify(messagesFor[partner[postData.id]]) + ' *** to id ' + postData.id)
webrtcResponse({
msgs: messagesFor[postData.id]
}, res)
messagesFor[postData.id] = []
}
exports.get = getMessages