-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathclient.js
More file actions
144 lines (125 loc) · 3.27 KB
/
Copy pathclient.js
File metadata and controls
144 lines (125 loc) · 3.27 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*!
* Pomelo -- commandLine Client
* Copyright(c) 2012 fantasyni <fantasyni@163.com>
* MIT Licensed
*/
var protocol = require('../util/protocol');
var io = require('socket.io-client');
var utils = require('../util/utils');
var Client = function(opt) {
this.id = "";
this.reqId = 1;
this.callbacks = {};
this.listeners = {};
this.state = Client.ST_INITED;
this.socket = null;
opt = opt || {};
this.username = opt['username'] || "";
this.password = opt['password'] || "";
this.md5 = opt['md5'] || false;
};
Client.prototype = {
connect: function(id, host, port, cb) {
this.id = id;
var self = this;
this.socket = io.connect('http://' + host + ':' + port, {
'force new connection': true,
'reconnect': false
});
this.socket.on('connect', function() {
self.state = Client.ST_CONNECTED;
if(self.md5){
self.password = utils.md5(self.password);
}
self.socket.emit('register', {
type: "client",
id: id,
username: self.username,
password: self.password,
md5: self.md5
});
});
this.socket.on('register', function(res) {
if (res.code !== protocol.PRO_OK) {
cb(res.msg);
return;
}
self.state = Client.ST_REGISTERED;
cb();
});
this.socket.on('client', function(msg) {
msg = protocol.parse(msg);
if (msg.respId) {
// response for request
var cb = self.callbacks[msg.respId];
delete self.callbacks[msg.respId];
if (cb && typeof cb === 'function') {
cb(msg.error, msg.body);
}
} else if (msg.moduleId) {
// notify
self.emit(msg.moduleId, msg);
}
});
this.socket.on('error', function(err) {
if (self.state < Client.ST_CONNECTED) {
cb(err);
}
self.emit('error', err);
});
this.socket.on('disconnect', function(reason) {
this.state = Client.ST_CLOSED;
self.emit('close');
});
},
request: function(moduleId, msg, cb) {
var id = this.reqId++;
// something dirty: attach current client id into msg
msg = msg || {};
msg.clientId = this.id;
msg.username = this.username;
var req = protocol.composeRequest(id, moduleId, msg);
this.callbacks[id] = cb;
this.socket.emit('client', req);
},
notify: function(moduleId, msg) {
// something dirty: attach current client id into msg
msg = msg || {};
msg.clientId = this.id;
msg.username = this.username;
var req = protocol.composeRequest(null, moduleId, msg);
this.socket.emit('client', req);
},
command: function(command, moduleId, msg, cb) {
var id = this.reqId++;
msg = msg || {};
msg.clientId = this.id;
msg.username = this.username;
var commandReq = protocol.composeCommand(id, command, moduleId, msg);
this.callbacks[id] = cb;
this.socket.emit('client', commandReq);
},
on: function(event, listener) {
this.listeners[event] = this.listeners[event] || [];
this.listeners[event].push(listener);
},
emit: function(event) {
var listeners = this.listeners[event];
if (!listeners || !listeners.length) {
return;
}
var args = Array.prototype.slice.call(arguments, 1);
var listener;
for (var i = 0, l = listeners.length; i < l; i++) {
listener = listeners[i];
if (typeof listener === 'function') {
listener.apply(null, args);
}
}
}
};
Client.ST_INITED = 1;
Client.ST_CONNECTED = 2;
Client.ST_REGISTERED = 3;
Client.ST_CLOSED = 4;
module.exports = Client;