shrub/main/lib/urb.js

183 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-07-17 06:10:13 +04:00
window.urb = {
ship: ship,
port: port,
auto: auto,
oryx: oryx,
user: user,
seqn_h: 0,
seqn_u: 0,
seqn_s: 0,
dely: 0,
puls: 0,
perms: {
pol:"gie",
2014-07-17 06:10:13 +04:00
sub:"tis",
uns:"tiu",
mes:"tim",
heb:"tih"
},
cabs: {},
req: function(method,url,params,json,cb) {
var xhr = new XMLHttpRequest()
xhr.open(method.toUpperCase(), url)
if(json)
xhr.setRequestHeader("content-type", "text/json")
_data = {}
if(params.data) { _data.data = params.data; }
if(params.ship) { _data.ship = params.ship; }
if(params.path) { _data.path = params.path; }
if(params.appl) { _data.appl = params.appl; }
__data = {oryx: oryx, xyro: _data}
xhr.send(JSON.stringify(__data))
if(cb) {
xhr.onload = function() {
cb(null,{
"status":this.status,
"data":JSON.parse(this.responseText)
})
}
xhr.onerror = function() {
cb({
"status":this.status,
"data":this.responseText
})
}
}
},
send: function(params,cb) {
if(!params)
throw new Error("You must supply params to urb.send.")
if(!params.appl)
throw new Error("You must specify an appl for urb.send.")
if(!params.data) { params.data = {}; }
var method, perm, url, $this
type = params.type ? params.type : "mes"
perm = this.perms[type]
params.ship = params.ship ? params.ship : this.ship
method = "put"
url = [perm,this.user,this.port,this.seqn_s]
url = "/"+url.join("/")
this.seqn_s++
$this = this
this.req(method,url,params,true,function(err,data) {
if(err) { $this.seqn_s--; }
if(cb) { cb.apply(this,arguments); }
})
},
2014-07-21 22:42:02 +04:00
subscribe: function(params,cb) {
2014-07-17 06:10:13 +04:00
if(!cb)
throw new Error("You must supply a callback to urb.subscribe.")
2014-07-21 22:42:02 +04:00
if(!params)
throw new Error("You must supply params to urb.subscribe.")
if(!params.appl)
throw new Error("You must specify an appl for urb.subscribe.")
if(!params.path)
throw new Error("You must specify a path for urb.subscribe.")
params.ship = params.ship ? params.ship : this.ship
2014-07-17 06:10:13 +04:00
var method, perm, url, $this
2014-07-21 22:42:02 +04:00
params.type = "sub"
params.incs = function() {
window.urb.seqn_u++
2014-07-17 06:10:13 +04:00
}
2014-07-21 22:42:02 +04:00
this.cabs[params.appl+","+params.path.replace(/[^\x00-\x7F]/g, "")+","+params.ship] = cb
2014-07-17 06:10:13 +04:00
2014-07-21 22:42:02 +04:00
url = [this.perms["sub"],this.user,this.port]
2014-07-17 06:10:13 +04:00
url = "/"+url.join("/")
2014-07-21 22:42:02 +04:00
method = "put"
2014-07-17 06:10:13 +04:00
$this = this
this.req(method,url,params,true,function(err,data) {
if(cb) { cb.call(this,err,{status: data.status, data: data.data.data})}
2014-07-17 06:10:13 +04:00
if(!err && $this.puls == 0) {
params.type = "pol"
$this.poll(params)
2014-07-17 06:10:13 +04:00
}
})
},
2014-07-21 22:42:02 +04:00
unsubscribe: function(params,cb) {
if(!params)
throw new Error("You must supply params to urb.unsubscribe.")
if(!params.appl)
throw new Error("You must specify an appl for urb.unsubscribe.")
if(!params.path)
throw new Error("You must specify a path for urb.unsubscribe.")
params.ship = params.ship ? params.ship : this.ship
2014-07-17 06:10:13 +04:00
method = "put"
type = "uns"
2014-07-21 22:42:02 +04:00
url = [this.perms[type],this.user,this.port]
2014-07-17 06:10:13 +04:00
url = "/"+url.join("/")
2014-07-18 23:23:35 +04:00
var $this = this
this.req(method,url,params,true,function(err,data) {
2014-07-21 22:42:02 +04:00
fn = params.appl+","+params.path.replace(/[^\x00-\x7F]/g, "")+","+params.ship
2014-07-18 23:23:35 +04:00
$this.cabs[fn]('subscription closed')
2014-07-17 06:10:13 +04:00
})
},
heartbeat: function() {
this.poll({
type:"heb",
ship:this.ship,
incs:function() {
window.urb.seqn_h++
2014-07-17 06:10:13 +04:00
}
},function() {
console.log('heartbeat.')
})
},
poll: function(params,cb) {
if(!params)
throw new Error("You must supply params to urb.poll.")
var method, perm, url, $this
method = "get"
type = params.type ? params.type : "pol"
2014-07-17 06:10:13 +04:00
perm = this.perms[type]
url = [perm,this.user,this.port,this.seqn_u]
2014-07-17 06:10:13 +04:00
url = "/"+url.join("/")
this.puls = 1
$this = this
this.req(method,url,params,false,function(err,data) {
if(cb) {
cb.call(this,err,{status: data.status, data: data.data.data})
2014-07-17 06:10:13 +04:00
} else {
fn = data.data.appl+","+data.data.path.replace(/[^\x00-\x7F]/g, "")
+","+data.data.ship
$this.cabs[fn].call(this,err,
{status: data.status, data: data.data.data})
2014-07-17 06:10:13 +04:00
}
if(err)
$this.dely += 1000
else {
$this.dely = 0
params.incs()
}
setTimeout(function() {
$this.poll(params,cb)
},$this.dely)
})
}
}