19 |
|
|
20 |
function Meteor(instID) { |
function Meteor(instID) { |
21 |
|
|
|
this.lastmsgreceived = -1; |
|
22 |
this.transferDoc = false; |
this.transferDoc = false; |
23 |
this.pingtimer = false; |
this.pingtimer = false; |
24 |
this.updatepollfreqtimer = false; |
this.updatepollfreqtimer = false; |
25 |
this.lastrequest = 0; |
this.lastrequest = 0; |
26 |
this.recvtimes = new Array(); |
this.recvtimes = []; |
27 |
this.MHostId = false; |
this.MHostId = false; |
28 |
this.callback_process = function() {}; |
this.callback_process = function() {}; |
29 |
this.callback_reset = function() {}; |
this.callback_reset = function() {}; |
32 |
this.callback_statuschanged = function() {}; |
this.callback_statuschanged = function() {}; |
33 |
this.persist = true; |
this.persist = true; |
34 |
this.frameloadtimer = false; |
this.frameloadtimer = false; |
|
this.frameurl = false; |
|
35 |
this.debugmode = false; |
this.debugmode = false; |
36 |
|
this.subsurl = false; |
37 |
|
this.channels = {}; |
38 |
|
|
39 |
// Documented public properties |
// Documented public properties |
|
this.channel = false; |
|
40 |
this.subdomain = "data"; |
this.subdomain = "data"; |
41 |
this.dynamicpageaddress = "push"; |
this.dynamicpageaddress = "push"; |
|
this.backtrack = 0; |
|
42 |
this.smartpoll = true; |
this.smartpoll = true; |
43 |
this.pollfreq = 2000; |
this.pollfreq = 2000; |
44 |
this.minpollfreq = 2000; |
this.minpollfreq = 2000; |
45 |
this.mode = "poll"; |
this.mode = "poll"; |
46 |
this.polltimeout=30000; |
this.polltimeout=30000; |
|
this.maxmessages=0; |
|
47 |
this.pingtimeout = 10000; |
this.pingtimeout = 10000; |
48 |
|
this.maxmessages = 0; |
49 |
this.status = 0; |
this.status = 0; |
50 |
|
|
51 |
/* Statuses: 0 = Uninitialised, |
/* Statuses: 0 = Uninitialised, |
81 |
if (this.debugmode) console.log("Frame registered"); |
if (this.debugmode) console.log("Frame registered"); |
82 |
} |
} |
83 |
|
|
84 |
|
Meteor.reset = function(ifr) { |
85 |
|
instid = new String(ifr.window.frameElement.id); |
86 |
|
instid = instid.replace(/.*_([0-9]*)$/, "$1"); |
87 |
|
this.instances[instid].reset(); |
88 |
|
} |
89 |
|
|
90 |
|
Meteor.prototype.joinChannel = function(channelname, backtrack) { |
91 |
|
if (typeof(this.channels[channelname]) != "undefined") throw "Cannot join channel "+channelname+": already subscribed"; |
92 |
|
this.channels[channelname] = {backtrack:backtrack, lastmsgreceived:0}; |
93 |
|
if (this.debugmode) console.log("Joined channel "+channelname+", channel list follows"); |
94 |
|
if (this.debugmode) console.log(this.channels); |
95 |
|
if (this.status != 0) this.start(); |
96 |
|
} |
97 |
|
|
98 |
|
Meteor.prototype.leaveChannel = function(channelname) { |
99 |
|
if (typeof(this.channels[channelname]) == "undefined") throw "Cannot leave channel "+channelname+": not subscribed"; |
100 |
|
delete this.channels[channelname]; |
101 |
|
if (this.status != 0) this.start(); |
102 |
|
} |
103 |
|
|
104 |
Meteor.prototype.start = function() { |
Meteor.prototype.start = function() { |
105 |
this.persist = (this.maxmessages)?1:0; |
this.persist = (this.maxmessages)?1:0; |
106 |
this.smartpoll = (this.smartpoll)?1:0; |
this.smartpoll = (this.smartpoll)?1:0; |
107 |
this.mode = (this.mode=="stream")?"stream":"poll"; |
this.mode = (this.mode=="stream")?"stream":"poll"; |
108 |
if (!this.subdomain || !this.channel) throw "Channel or Meteor subdomain host not specified"; |
if (!this.subdomain || this.channels.length) throw "Channel or Meteor subdomain host not specified"; |
109 |
this.stop(); |
this.stop(); |
110 |
var now = new Date(); |
var now = new Date(); |
111 |
var t = now.getTime(); |
var t = now.getTime(); |
112 |
this.setstatus(1); |
this.setstatus(1); |
113 |
if (this.mode=="stream") { |
var surl = "http://" + this.subdomain + "." + location.hostname + "/" + this.dynamicpageaddress + "?id=" + this.MHostId; |
114 |
var surl = "http://"+this.subdomain+"."+location.hostname+"/"+this.dynamicpageaddress+"?channel="+this.channel+"&id="+this.MHostId; |
if (this.maxmessages && !this.persist) surl += "&maxmessages=" + this.maxmessages; |
115 |
if (this.lastmsgreceived >= 0) { |
for (var c in this.channels) { |
116 |
surl += "&restartfrom="+this.lastmsgreceived; |
surl += "&channel="+c; |
117 |
} else if (this.backtrack > 0) { |
if (this.channels[c].lastmsgreceived >= 0) { |
118 |
surl += "&backtrack="+this.backtrack; |
surl += "&restartfrom="+this.channels[c].lastmsgreceived; |
119 |
} else if (this.backtrack < 0 || isNaN(this.backtrack)) { |
} else if (this.channels[c].backtrack > 0) { |
120 |
|
surl += "&backtrack="+this.channels[c].backtrack; |
121 |
|
} else if (this.channels[c].backtrack < 0 || isNaN(this.channels[c].backtrack)) { |
122 |
surl += "&restartfrom="; |
surl += "&restartfrom="; |
123 |
} |
} |
124 |
this.createIframe(surl); |
} |
125 |
|
this.subsurl = surl; |
126 |
|
if (this.mode=="stream") { |
127 |
|
this.createIframe(this.subsurl); |
128 |
var f = this.pollmode.bind(this); |
var f = this.pollmode.bind(this); |
129 |
clearTimeout(this.pingtimer); |
clearTimeout(this.pingtimer); |
130 |
this.pingtimer = setTimeout(f, this.pingtimeout); |
this.pingtimer = setTimeout(f, this.pingtimeout); |
189 |
|
|
190 |
Meteor.prototype.pollmode = function() { |
Meteor.prototype.pollmode = function() { |
191 |
if (this.debugmode) console.log("Ping timeout"); |
if (this.debugmode) console.log("Ping timeout"); |
|
this.stop(); |
|
192 |
this.mode="poll"; |
this.mode="poll"; |
193 |
this.start(); |
this.start(); |
194 |
this.callback_changemode("poll"); |
this.callback_changemode("poll"); |
195 |
this.lastpingtime = false; |
this.lastpingtime = false; |
196 |
} |
} |
197 |
|
|
198 |
Meteor.prototype.process = function(id, data, timestamp) { |
Meteor.prototype.process = function(id, channel, data) { |
199 |
if (id > this.lastmsgreceived) { |
if (id == -1) { |
200 |
this.callback_process(data, timestamp); |
if (this.debugmode) console.log("Ping"); |
201 |
if (id != -1) this.lastmsgreceived = id; |
this.ping(); |
202 |
|
} else if (typeof(this.channels[channel]) != "undefined" && id > this.channels[channel].lastmsgreceived) { |
203 |
|
if (this.debugmode) console.log("Message "+id+" received on channel "+channel+" (last id on channel: "+this.channels[channel].lastmsgreceived+")\n"+data); |
204 |
|
this.callback_process(data); |
205 |
|
this.channels[channel].lastmsgreceived = id; |
206 |
if (this.mode=="poll") { |
if (this.mode=="poll") { |
207 |
var now = new Date(); |
var now = new Date(); |
208 |
var t = now.getTime(); |
var t = now.getTime(); |
209 |
this.recvtimes[this.recvtimes.length] = t; |
this.recvtimes[this.recvtimes.length] = t; |
210 |
while (this.recvtimes.length > 5) this.recvtimes.shift(); |
while (this.recvtimes.length > 5) this.recvtimes.shift(); |
211 |
} |
} |
|
} else if (id == -1) { |
|
|
this.ping(); |
|
212 |
} |
} |
213 |
this.setstatus(5); |
this.setstatus(5); |
214 |
} |
} |