forked from jun-lu/webchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvchat-rtc.js
227 lines (187 loc) · 4.29 KB
/
vchat-rtc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
为vchat.co提供视频服务
vchat-rtc.js
*/
var WebSocketServer = require('ws').Server;
var config = require("./config");
var UserModel = require("./lib/UserModel");
var User = require("./lib/User");
var session = require("./routes/session");
var Promise = require("./lib/Promise");
var wss = null;
var socketHashList = {
list:{},
//添加到队列
add:function(roomid, socket){
var list = this[roomid];
if( list ){
list.push( socket );
}else{
this[roomid] = [socket];
}
},
//从队列中删除
remove:function(roomid, socket){
var list = this[roomid];
for(var i=0; list && i<list.length; i++){
if(list[i] == socket){
list.splice(i, 1);
break;
}
}
},
//消息分发
//发给指定房间的某个人,如果id为空 发给房间的所有人
distribute:function( roomid, data, id ){
var list = this[roomid];
//console.log("distribute start", roomid, id)
for(var i=0; list && i<list.length; i++){
if( id == undefined || String(list[i].session.user._id) == id){
//console.log("distribute ok", roomid, id)
list[i].send( JSON.stringify(data) );//.toString()
}
}
},
getUserList:function( roomid ){
var list = this[roomid] || [];
var userlist = [];
var map = {};
var user = null;
for(var i=0; i<list.length; i++){
user = list[i].session.user.getPublicInfo(36);
if( map[user._id] == undefined){//去重复
userlist.push( user );
map[user._id] = 1;
}
}
return userlist;
},
//如果在多个客户端登录会有同一个人的多个连接
hasOnline:function(roomid, _id){
var list = this[roomid] || [];
for(var i=0; i<list.length; i++){
if( list[i].session.user._id == _id){
return true;
}
}
return false;
}
};
/**
{
type:""//事件event
data:{}//数据对象
}
*/
wss = new WebSocketServer({
port:8001
});
wss.on("error", function(){
console.log("err", arguments)
});
/**
识别用户身份
*/
wss.on('connection', function( ws ){
//console.log("123456789")
//console.log("ws", ws.upgradeReq);
//ws.session.user.clientid = ++clientid;
ws.roomid = null;
ws.on("error", function(){});
ws.on("close", function(){
socketHashList.remove( ws.roomid, ws );
socketHashList.distribute( ws.roomid, {
type:"offline",
data:ws.session.user.getPublicInfo(36)
});
});
ws.on("message", function( message ){
console.log("onmessage", message);
var socketMessage = JSON.parse( message );
this.emit( socketMessage.type, socketMessage.data);
});
ws.on("login", function( data ){
/**
data = {
sid:
roomid
}
*/
var sid = data.sid;
var roomid = data.roomid;
var promise = new Promise();
ws.roomid = roomid;
//验证用户
promise.then(function(){
session.verificationSID( sid, function( status ){
if( status.code == 0 ){
var user = User.factory(status.result);
ws.id = status.result._id;
ws.session = {
user:user
};
socketHashList.add( roomid, ws );//加入到在线列表
ws.send( JSON.stringify(data) );
promise.ok( user );
}else{
ws.send( JSON.stringify({
type:"login",
data:null
}) );
//ws.setClose();
}
});
});
//发送在线用户
promise.then(function( user ){
//通知所有人,我上线
socketHashList.distribute(ws.roomid, {
"type":"online",
"data":user.getPublicInfo(36)
});
ws.send(JSON.stringify({
type:"onlines",
data:{
"connections":socketHashList.getUserList( roomid ),
"me":user.getPublicInfo(36)
}
}));
});
promise.start();
});
//有人报告offer
ws.on("send_offer", function( data ){
var id = data.id;
//报告给对方我的 对外端口于ip
socketHashList.distribute(ws.roomid, {
"type":"receive_offer",
"data":{
"sdp":data.sdp,
"id":ws.id
}
}, id);
});
//对方受到offer,再报告回来
ws.on("send_answer", function( data ){
var id = data.id;
socketHashList.distribute(ws.roomid, {
"type":"receive_answer",
"data":{
"sdp":data.sdp,
"id":ws.id
}
}, id);
});
ws.on("send_ice_candidate", function( data ){
var id = data.id;
//报告给对方我的 对外端口于ip
socketHashList.distribute(ws.roomid, {
"type":"receive_ice_candidate",
"data":{
"label":data.label,
"candidate":data.candidate,
"id":ws.id
}
}, id);
});
});