-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.js
173 lines (151 loc) · 4.22 KB
/
client.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
var dgram = require('dgram');
var client = dgram.createSocket("udp4");
var protocol = require('pomelo-protocol');
var Package = protocol.Package;
var Message = protocol.Message;
var reqId = 0;
var nextHeartbeatTimeout = 0;
var gapThreshold = 100;
var heartbeatInterval = 3000;
var heartbeatTimeout = 6000;
var heartbeatTimeoutId = null;
var handshakeCallback = null;
var heartbeatId = null;
var callbacks = {};
var host = 'localhost';
var port = 3010;
var handshakeBuffer = {
'sys': {
type: 'udp-client',
version: '0.0.1',
rsa: {}
},
'user': {
}
};
var heartbeatData = Package.encode(Package.TYPE_HEARTBEAT);
var handshakeAckData = Package.encode(Package.TYPE_HANDSHAKE_ACK);
var handshakeData = Package.encode(Package.TYPE_HANDSHAKE, protocol.strencode(JSON.stringify(handshakeBuffer)));
var init = function(ip, pt, cb) {
host = ip;
port = pt;
sendHandshake(cb);
};
var send = function(data, cb) {
client.send(data, 0, data.length, port, host, function(err, bytes) {
if(!!err) {
console.error('udp client send message with error: %j', err.stack);
}
if(!!cb) {
process.nextTick(cb);
}
});
};
var sendHandshake = function(cb) {
send(handshakeData);
handshakeCallback = cb;
};
var decode = function(data) {
var msg = Message.decode(data);
msg.body = JSON.parse(protocol.strdecode(msg.body));
return msg;
};
var onData = function(data) {
var msg = decode(data);
processMessage(msg);
};
var processMessage = function(msg) {
if(!msg.id) {
client.emit(msg.route, msg.body);
return;
}
var cb = callbacks[msg.id];
delete callbacks[msg.id];
cb(msg.body);
return;
};
var onKick = function(data) {
data = JSON.parse(protocol.strdecode(data));
console.log('receive kick data: %j', data);
};
var sendMessage = function(reqId, route, msg) {
msg = protocol.strencode(JSON.stringify(msg));
msg = Message.encode(reqId, Message.TYPE_REQUEST, 0, route, msg);
var packet = Package.encode(Package.TYPE_DATA, msg);
send(packet);
};
var handshake = function(data) {
data = JSON.parse(protocol.strdecode(data));
console.log('receive handshake data: %j', data);
send(handshakeAckData, handshakeCallback);
};
var request = function(route, message, cb) {
reqId++;
callbacks[reqId] = cb;
sendMessage(reqId, route, message);
};
var heartbeat = function(data) {
console.log('receive heartbeat');
if(!heartbeatInterval) {
return;
}
if(heartbeatTimeoutId) {
clearTimeout(heartbeatTimeoutId);
heartbeatTimeoutId = null;
}
if(heartbeatId) {
return;
}
heartbeatId = setTimeout(function() {
heartbeatId = null;
console.log('send heartbeat message');
send(heartbeatData);
nextHeartbeatTimeout = Date.now() + heartbeatTimeout;
heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, heartbeatTimeout);
}, heartbeatInterval);
};
var heartbeatTimeoutCb = function() {
var gap = nextHeartbeatTimeout - Date.now();
if(gap > gapThreshold) {
heartbeatTimeoutId = setTimeout(heartbeatTimeoutCb, gap);
} else {
console.error('server heartbeat timeout');
}
};
var processPackage = function(msgs) {
if(Array.isArray(msgs)) {
for(var i=0; i<msgs.length; i++) {
var msg = msgs[i];
handlers[msg.type](msg.body);
}
} else {
handlers[msgs.type](msgs.body);
}
};
handlers = {};
handlers[Package.TYPE_HANDSHAKE] = handshake;
handlers[Package.TYPE_HEARTBEAT] = heartbeat;
handlers[Package.TYPE_DATA] = onData;
handlers[Package.TYPE_KICK] = onKick;
client.on("message", function (msg, rinfo) {
processPackage(Package.decode(msg));
});
init('localhost', 3010, function() {
request('connector.entryHandler.entry', {username:'py', rid:'1', route:'connector.entryHandler.entry'}, function(data) {
console.log('receive enter callback data: %j', data);
/**
request('chat.chatHandler.send', {content: 'hello world', target: '*', route: 'onChat'}, function(data) {
console.log('receive send callback data: %j', data);
});
*/
});
client.on('onAdd', function(msg) {
console.log('onAdd receive message: %j', msg);
});
client.on('onLeave', function(msg) {
console.log('onLeave receive message: %j', msg);
});
client.on('onChat', function(msg) {
console.log('onChat receive message: %j', msg);
});
});