shrub/main/lib/urb.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-08-01 03:57:35 +04:00
window.urb.seqn_s = 0
window.urb.send = function(params,cb) {
if(!params)
throw new Error("You must supply params to urb.send.")
if(!params.appl) {
if(!urb.appl)
throw new Error("You must specify an appl for urb.send.")
params.appl = urb.appl
}
2014-08-01 03:57:35 +04:00
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
2014-10-24 01:09:39 +04:00
params.mark = params.mark ? params.mark : "json"
2014-08-01 03:57:35 +04:00
method = "put"
url = [perm,this.user,this.port,this.seqn_s]
url = "/"+url.join("/")
this.seqn_s++
$this = this
this.qreq(method,url,params,true,function(err,data) {
if(err) { $this.seqn_s--; }
if(cb) { cb.apply(this,arguments); }
})
}
window.urb.subscribe = function(params,cb) {
if(!cb)
throw new Error("You must supply a callback to urb.subscribe.")
if(!params)
throw new Error("You must supply params to urb.subscribe.")
if(!params.appl) {
if(!urb.appl)
throw new Error("You must specify an appl for urb.subscribe.")
params.appl = urb.appl
}
2014-08-01 03:57:35 +04:00
if(!params.path)
throw new Error("You must specify a path for urb.subscribe.")
params.ship = params.ship ? params.ship : this.ship
var method, perm, url, $this
params.type = "sub"
this.cabs[this.gsig(params)] = cb
url = [this.perms["sub"],this.user,this.port]
url = "/"+url.join("/")
method = "put"
$this = this
this.qreq(method,url,params,true,function(err,data) {
2014-08-05 22:45:59 +04:00
if(cb) { cb.apply(this,[err,{status: data.status, data: data.data}])}
2014-08-01 03:57:35 +04:00
if(!err && $this.puls == 0) {
params.type = "pol"
$this.poll(params)
2014-07-17 06:10:13 +04:00
}
2014-08-01 03:57:35 +04:00
})
2014-07-17 06:10:13 +04:00
}
2014-07-29 03:17:59 +04:00
2014-08-01 03:57:35 +04:00
window.urb.unsubscribe = function(params,cb) {
if(!params)
throw new Error("You must supply params to urb.unsubscribe.")
if(!params.appl) {
if(!urb.appl)
throw new Error("You must specify an appl for urb.unsubscribe.")
params.appl = urb.appl
}
2014-08-01 03:57:35 +04:00
if(!params.path)
throw new Error("You must specify a path for urb.unsubscribe.")
params.ship = params.ship ? params.ship : this.ship
method = "put"
type = "uns"
url = [this.perms[type],this.user,this.port]
url = "/"+url.join("/")
var $this = this
this.req(method,url,params,true,function(err,data) {
2014-12-19 22:03:01 +03:00
cb(err,data)
2014-08-01 03:57:35 +04:00
})
}
2014-12-10 02:51:18 +03:00
window.urb.util = {
toDate: function (dat){
var mils = Math.floor((0x10000 * dat.getUTCMilliseconds()) / 1000).toString(16)
2015-01-09 22:57:15 +03:00
function pad(num, str){
return ((new Array(num + 1)).join('0') + str).substr(-num,num)
}
2014-12-10 02:51:18 +03:00
return '~' + dat.getUTCFullYear() +
'.' + (dat.getUTCMonth() + 1) +
'.' + dat.getUTCDate() +
2015-01-09 22:57:15 +03:00
'..' + pad(2, dat.getUTCHours()) +
'.' + pad(2, dat.getUTCMinutes()) +
'.' + pad(2, dat.getUTCSeconds()) +
'..' + pad(4, mils)
2014-12-10 02:51:18 +03:00
}
}