-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrealtime-channel.js
179 lines (159 loc) · 5.26 KB
/
realtime-channel.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
var realtime = realtime || {};
realtime.channel = realtime.channel || {};
!function(factory) {
factory(Paho.MQTT);
}(function(Mqtt) {
realtime.channel.Bus = function(host, port, clientId, userName, password) {
var that = this;
var mqtt = new Mqtt.Client(host, port, clientId);
var onConnect = function() {
state = realtime.channel.Bus.OPEN;
if (that.onopen) {
that.onopen();
}
};
var state = realtime.channel.Bus.CONNECTING;
mqtt.connect({onSuccess:onConnect, userName: userName, password: password});
var handlerMap = {};
var replyHandlers = {};
that.onopen = null;
that.onclose = null;
that.send = function(topic, payload, options, replyHandler) {
sendOrPub(true, topic, payload, options, replyHandler)
};
that.publish = function(topic, payload, options) {
sendOrPub(false, topic, payload, options, null)
};
that.subscribe = function(topic, handler) {
checkSpecified("topic", "string", topic);
checkSpecified("handler", "function", handler);
checkOpen();
var handlers = handlerMap[topic];
if (!handlers) {
handlers = [handler];
handlerMap[topic] = handlers;
// First handler for this topic so we should register the connection
mqtt.subscribe(topic);
} else {
handlers[handlers.length] = handler;
}
var unsubscribe = function() {
//checkSpecified("topic", 'string', topic);
//checkSpecified("handler", 'function', handler);
checkOpen();
var handlers = handlerMap[topic];
if (handlers) {
var idx = handlers.indexOf(handler);
if (idx != -1) handlers.splice(idx, 1);
if (handlers.length == 0) {
// No more local handlers so we should unregister the connection
mqtt.unsubscribe(topic);
delete handlerMap[topic];
}
}
};
return {unsubscribe: unsubscribe};
};
that.close = function() {
checkOpen();
state = realtime.channel.Bus.CLOSING;
mqtt.disconnect();
};
that.readyState = function() {
return state;
};
// called when the client loses its connection
mqtt.onConnectionLost = function(responseObject) {
state = realtime.channel.Bus.CLOSED;
if (that.onclose) {
that.onclose(responseObject);
}
};
mqtt.onMessageArrived = function(e) {
var payloadString = e.payloadString;
var topic = e.destinationName;
var message = {};
message['payload'] = JSON.parse(payloadString);
if (message["error"]) {
console.error("Error received: " + JSON.stringify(message["error"]));
return;
}
message["topic"] = topic;
var replyTopic = message["replyTopic"];
if (replyTopic) {
message.reply = function(reply, replyHandler) {
// Send back reply
that.send(replyTopic, reply, replyHandler);
};
}
var handlers = handlerMap[topic];
if (handlers) {
// We make a copy since the handler might get unregistered from within the
// handler itself, which would screw up our iteration
var copy = handlers.slice(0);
for (var i = 0; i < copy.length; i++) {
copy[i](message);
}
} else {
// Might be a reply message
var handler = replyHandlers[topic];
if (handler) {
delete replyHandlers[topic];
mqtt.unsubscribe(topic);
handler({"result": message});
}
}
};
function sendOrPub(send, topic, payload, options, replyHandler) {
if (replyHandler === undefined && typeof options == "function") {
replyHandler = options;
options = null;
}
checkSpecified("topic", 'string', topic);
checkSpecified("replyHandler", 'function', replyHandler, true);
checkOpen();
var msg = {};
if (send) {
msg["send"] = true;
}
if (payload) {
msg["payload"] = payload;
}
if (options) {
msg["options"] = options;
}
if (replyHandler) {
var replyTopic = makeUUID(topic);
msg["replyTopic"] = replyTopic;
replyHandlers[replyTopic] = replyHandler;
mqtt.subscribe(replyTopic);
}
var message = new Mqtt.Message(JSON.stringify(msg));
message.destinationName = topic;
mqtt.send(message);
}
function checkOpen() {
if (state != realtime.channel.Bus.OPEN) {
throw new Error('INVALID_STATE_ERR');
}
}
function checkSpecified(paramName, paramType, param, optional) {
if (!optional && !param) {
throw new Error("Parameter " + paramName + " must be specified");
}
if (param && typeof param != paramType) {
throw new Error("Parameter " + paramName + " must be of type " + paramType);
}
}
function makeUUID(topic){
var id = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
.replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)});
return "reply/" + id + "/" + topic;
}
};
realtime.channel.Bus.CONNECTING = 0;
realtime.channel.Bus.OPEN = 1;
realtime.channel.Bus.CLOSING = 2;
realtime.channel.Bus.CLOSED = 3;
return realtime.channel.Bus;
});