-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
213 lines (195 loc) · 6.95 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
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
/**
@module Server
*/
'use strict';
var
co = require('co'),
amqpUtils = require('./amqpUtils');
function isGenerator(obj) {
return obj && 'function' == typeof obj.next && 'function' == typeof obj.throw;
}
/**
ref:https://github.com/postwait/node-amqp
1.用户发送消息队列 message -> queue -> out , queue name : conf.messageQueue , default:messageQueue
2.接受消息队列 out -> queue -> message ; exchange name : conf.receiveExchange , default:receiveExchange --> receiveQueue
3.连接状态队列 status -> queue -> message ; exchange name : conf statusExchange , default: statusExchange
@constructor
*/
function MqManager(conf){
this.conf = conf;
this.messageCb = null;
this.conf.receiveExchange = this.conf.receiveExchange || 'receiveExchange';
this.conf.receiveQueue = this.conf.receiveQueue || 'receiveQueue';
this.conf.statusExchange = this.conf.statusExchange || 'statusExchange';
this.conf.statusQueue = this.conf.statusQueue || 'statusQueue';
this.conf.messageQueue = this.conf.messageQueue || 'messageQueue';
this.conf.messageExchange = this.conf.messageExchange || 'messageExchange';
}
MqManager.prototype.init = function*(){
this.client = yield amqpUtils.createMqClient(this.conf);
this.receiveExchange = yield amqpUtils.createExchange(this.client , this.conf.receiveExchange, {type:'direct'});
this.statusExchange = yield amqpUtils.createExchange(this.client , this.conf.statusExchange ,{type:'direct'});
var messageExchange = yield amqpUtils.createExchange(this.client , this.conf.messageExchange ,{type:'fanout'});
this.messageQueue = yield amqpUtils.createQueue(this.client ,this.conf.messageQueue+"-"+Math.random());
yield amqpUtils.queueBind(this.messageQueue ,messageExchange, this.conf.messageQueue);
// this.messageQueue.bind(this.conf.messageExchange, this.conf.messageQueue);
var _this = this;
this.messageQueue.subscribe(function (message, headers, deliveryInfo) {
_this.fireMessage(message,headers);
});
console.log('init mq over')
};
MqManager.prototype.fireMessage = function(message,headers){
if(this.messageCb){
this.messageCb(message,headers);
}else{
console.warn('no message callback ' , message);
}
};
/**
put message to the send queue
*/
MqManager.prototype.send = function(message, headers){
this.receiveExchange.publish(this.conf.receiveQueue, message ,{contentType:'application/json' , headers:headers||{}});
};
/**
put connection status change message to the send status queue
*/
MqManager.prototype.sendStatusMessage = function(message,headers){
this.statusExchange.publish(this.conf.statusQueue, message ,{contentType:'application/json',headers:headers||{}});
};
/**
receive a new message from message queue
*/
MqManager.prototype.onMessage = function(cb){
this.messageCb = cb;
};
/**
ref: https://github.com/Worlize/WebSocket-Node
WebSocket manager
manager the connections
1.send message
@constructor
@param {object} conf - WsMangaer config ref https://github.com/Worlize/WebSocket-Node/wiki/Documentation
@param {function} conf.keyFn - function*(request) , give key for the new connection;
@param {function} conf.messageKeyFn - function*(message) , give key from new message , the message is sended from client,
*/
function WsManager(conf){
if(!conf.keyFn){
throw new Error('WsManager conf must have a keyFn.');
}
if(!conf.messageKeyFn){
throw new Error('WsManager conf must have a messageKeyFn.');
}
this.conf = conf;
this.conf.autoAcceptConnections = this.conf.autoAcceptConnections || false;
this.server = new (require('websocket').server)(this.conf);
this.connections = {};
this.server.on('request' , this._onRequest.bind(this));
this.onMessageCb = null;
this.onConnectionStatusChangeCb = null;
this.onSendFailedCb = null;
this.connectionCount =0;
}
WsManager.prototype._onRequest = function(request){
co(function*(){
var key ;
try{
key = yield this.conf.keyFn(request,this);
if(!key){
return request.reject(404,'bad request');
}
}catch(e){
return request.reject(e.status||404, e.reason);
}
if(this.connections[key] && -1 == this.connections[key].closeReasonCode){
this.connections[key].old=true;
this.connections[key].drop();
// this.onConnectionStatusChangeCb({status:0,key:key , reasonCode:1002,description:"OldConnection"},this.connections[key].request.httpRequest.headers);
}
var connection = request.accept(this.conf.protocol,this.conf.origin||request.origin );
connection.request = request;
this.connections[key] = connection;
this.connectionCount +=1;
connection.__key__ = key;
if(this.onConnectionStatusChangeCb){
this.onConnectionStatusChangeCb({status:1,key:key},request.httpRequest.headers);
}
var _this = this;
connection.on('message' , function(message){
// _this.doBeforeAddToMq(message, this , function(e,message){
// if(e){
// return;
// }
if (message.type === 'utf8') {
if(_this.onMessageCb){
_this.onMessageCb(message.utf8Data,this.request.httpRequest.headers);
}else{
console.warn('new message not handled' , message);
}
}
// });
});
connection.on('close', function(reasonCode, description) {
if(this.old) {
return;
}
if(_this.onConnectionStatusChangeCb){
_this.onConnectionStatusChangeCb({status:0,key:this.__key__ , reasonCode:reasonCode,description:description},this.request.httpRequest.headers);
}
delete _this.connections[this.__key__];
// if(1000 != reasonCode){
//
// }
_this.connectionCount -=1;
});
}).call(this);
};
WsManager.prototype.send = function(message,headers){
var key = this.conf.messageKeyFn(message,headers);
var connection = this.connections[key];
if(connection){
if('string' != typeof message){
message = JSON.stringify(message);
}
try{
connection.sendUTF(message);
}catch(e){
console.error("send message failed" , message);
console.error(e);
if(this.onSendFailedCb){
this.onSendFailedCb(e,message,headers);
}
}
}
};
WsManager.prototype.onSendFailed = function(cb){
this.onSendFailedCb = cb;
};
WsManager.prototype.onMessage = function(cb){
this.onMessageCb = cb;
};
WsManager.prototype.onConnectionStatusChange = function(cb){
this.onConnectionStatusChangeCb = cb;
};
/**
wcConf -> for websocket
mqConf -> for rabbitmq
con -> for Server
*/
exports.start = function*(mqConf,wsConf){
var mqManager = new MqManager(mqConf);
yield mqManager.init();
var wsManager = new WsManager(wsConf);
//queue -> message -> ws
mqManager.onMessage(function(message,headers){
wsManager.send(message,headers);
});
wsManager.onMessage(function(message,headers){
mqManager.send(message,headers);
});
wsManager.onConnectionStatusChange(function(message,headers){
mqManager.sendStatusMessage(message,headers);
});
// return new Server(mqManager , wsManager, conf);
};