-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
78 lines (70 loc) · 2.25 KB
/
browser.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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
var Client = function (address, onsuccess, onerror) {
this.consumers = {};
this.publishCallbacks = {};
try {
this.ws = new WebSocket(address);
var self = this;
this.ws.onopen = function(){
setInterval(function(){
self.ws.send(JSON.stringify({
type: "publish",
data: {
topic: "session::update"
}
}));
},2000);
onsuccess = onsuccess || function (ctx) { console.log(ctx); };
onsuccess(self);
};
this.ws.onmessage = function(msg){
self._handleIncome(msg);
};
} catch (e) {
onerror = onerror || function (exception) { console.error(exception); };
onerror(e);
}
};
Client.prototype.registerConsumer = function(topic,cb) {
var consumers = this.consumers[topic] || [];
consumers.push(cb);
if(consumers.length === 1){
this.ws.send(JSON.stringify({
type: "registerConsumer",
data: {
topic: topic
}
}));
}
this.consumers[topic] = consumers;
};
Client.prototype.publish = function(topic,payload,cb) {
var id = ''+Date.now();
this.publishCallbacks[id] = cb;
this.ws.send(JSON.stringify({
type: 'publish',
data: {
topic: topic,
payload: payload,
id: id
},
}));
};
Client.prototype._handleIncome = function(msg){
var packet = JSON.parse(msg.data);
if(packet.type === 'consumerEvent'){
var consumers = this.consumers[packet.data.topic];
for (var i = consumers.length - 1; i >= 0; i--) {
consumers[i](packet.data);
}
}
if (packet.type === 'ack'){
if(packet.data.id && this.publishCallbacks[packet.data.id]){
this.publishCallbacks[packet.data.id](packet.data);
delete this.publishCallbacks[packet.data.id];
}
}
};
module.exports = Client;
},{}]},{},[1]);