-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtmsclient.js
126 lines (106 loc) · 3.23 KB
/
tmsclient.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
exports.create = function(url, token, callback) {
if (typeof(token) === 'function') {
callback = token;
token = undefined;
}
return WEBSOCKETCLIENT(function(client) {
var publishers = {};
var subscribers = {};
var callbacks = {};
var isopen = false;
var callbackid = 0;
var timeout;
if (token)
client.headers['x-token'] = token;
client.options.reconnectserver = true;
client.connect(url.replace(/^http/, 'ws'));
client.ready = false;
client.on('destroy', function() {
publishers = null;
subscribers = null;
for (var key in callbacks) {
var item = callbacks[key];
clearTimeout(item.timeout);
item.callback && item.callback('TMS has been destroyed');
}
callbacks = null;
timeout && clearTimeout(timeout);
timeout = null;
});
client.on('close', function() {
isopen = false;
client.ready = false;
});
client.on('message', function(msg) {
if (msg.type === 'call') {
if (callbacks[msg.callbackid]) {
var tmp = callbacks[msg.callbackid];
tmp.callback(msg.error ? ErrorBuilder.assign(msg.data) : null, msg.success ? msg.data : null);
tmp.timeout && clearTimeout(tmp.timeout);
delete callbacks[msg.callbackid];
}
} else if (msg.type === 'publish' && subscribers[msg.id] && publishers[msg.id]) {
var err = new ErrorBuilder();
var data = framework_jsonschema.transform(publishers[msg.id], err, msg.data, true);
if (data) {
for (var fn of subscribers[msg.id])
fn(data);
}
} else if (msg.type === 'meta') {
publishers = {};
for (var item of msg.publish)
publishers[item.id] = item.schema;
sync_subscribers();
isopen = true;
client.ready = true;
client.meta = msg;
if (callback) {
setImmediate(callback, null, client, client.meta);
callback = null;
}
client.emit('meta', msg);
client.emit('ready');
}
});
var timeouthandler = function(id) {
var obj = callbacks[id];
obj.callback && obj.callback('408: Timeout');
delete callbacks[id];
};
client.call = function(name, data, callback, timeout) {
if (callback)
client._call(name, data, callback, timeout);
else
return new Promise((resolve, reject) => client._call(name, data, (err, res) => err ? reject(err) : resolve(res), timeout));
};
client._call = function(name, data, callback, timeout) {
if (isopen) {
var key = (callbackid++) + '';
var obj = {};
obj.callback = callback;
obj.timeout = setTimeout(timeouthandler, timeout || 10000, key);
callbacks[key] = obj;
client.send({ type: 'call', id: name, data: data, callbackid: key });
} else
callback('TMS is offline');
};
client.subscribe = function(name, callback) {
timeout && clearTimeout(timeout);
timeout = setTimeout(sync_subscribers, 30, true);
if (subscribers[name])
subscribers[name].push(callback);
else
subscribers[name] = [callback];
};
client.publish = function(name, data) {
isopen && client.send({ type: 'subscribe', id: name, data: data });
};
var sync_subscribers = function(force) {
timeout && clearTimeout(timeout);
timeout = null;
var keys = Object.keys(subscribers);
if (force || keys.length)
client.send({ type: 'subscribers', subscribers: keys });
};
});
};