work in progress: Dojo client for talk protocol with long-poll transport

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3847 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2010-05-04 08:41:03 +00:00
parent 0b0a20083b
commit c4a0d2a6d2

View file

@ -27,98 +27,89 @@ dojo.declare('jocy.talk.Connection', null, {
info: 'jocy.talk Connection', info: 'jocy.talk Connection',
constructor: function() { constructor: function(url, name) {
this.url = url;
this.connected = false;
this.receiver = null; this.receiver = null;
this.polling = false; this.receiving = false;
this.id = null; this.id = (name == undefined | name == '' ? null : name);
}, },
open: function(receiver, id) { connect: function() {
this.receiver = receiver;
if (id != undefined) {
this.id = id;
} else {
this._getid();
}
this._poll();
return this.id;
},
close: function() {
return dojo.xhrPost({ return dojo.xhrPost({
url: '/.stop/' + this.id, url: this.url + '/.talk/connect',
postData: (this.id == null ? '' : dojo.toJson({name: this.id})),
handleAs: 'json',
error: dojo.hitch(this, this._handleError),
load: dojo.hitch(this, function(response, ioArgs) { load: dojo.hitch(this, function(response, ioArgs) {
this.polling = false; console.log(response);
this.id = response.id;
this.connected = true;
}) })
}); });
}, },
send: function(path, data) { receive: function(timeout) {
if (this.receiving) {
return;
}
this.receiving = true;
return dojo.xhrPost({ return dojo.xhrPost({
url: '/.send/' + this.id + path, url: this.url + '/.talk/receive/' + this.id,
postData: dojo.toJson(data) handleAs: 'json',
error: dojo.hitch(this, this._handleReceiveError),
load: dojo.hitch(this, this._handleReceiveResponse)
});
},
send: function(receiver, data) {
return dojo.xhrPost({
url: this.url + '/.talk/send/' + receiver,
postData: dojo.toJson(data),
handleAs: 'json',
error: dojo.hitch(this, this._handleError),
load: dojo.hitch(this, function(response, ioArgs) {
console.log(response);
})
});
},
disconnect: function() {
this.connected = false;
return dojo.xhrPost({
url: '/.talk/disconnect/' + this.id,
handleAs: 'json',
error: dojo.hitch(this, this._handleError),
load: dojo.hitch(this, function(response, ioArgs) {
console.log(response);
this.receiving = false;
})
}); });
}, },
// private methods // private methods
_getId: function() { _handleReceiveResponse: function(response, ioArgs) {
return dojo.xhrPost({ console.log(response);
url: '/.getid', if (!this.receiving) {
sync: true,
load: dojo.hitch(this, function(response, ioArgs) {
this.id = response;
})
});
},
_poll: function() {
if (this.polling) {
return; return;
} }
if (this.id == undefined) { this.receiving = false;
this._getId(); st = response.state;
}
this.polling = true;
return dojo.xhrPost({
url: '/.poll/' + this.id,
handleAs: 'json',
error: dojo.hitch(this, this._handlePollError),
load: dojo.hitch(this, this._handlePollResponse)
});
}, },
_handlePollResponse: function(response, ioArgs) { _handleReceiveError: function(response, ioArgs) {
if (!this.polling) { console.log(response);
if (!this.receiving) {
return; return;
} }
this.polling = false; this.receiving = false;
t = response.type; //this._poll();
switch (response.type) {
case 'stop':
break;
case 'idle':
this._poll();
break;
case 'error':
self.receiver(response.data);
break;
case 'data':
this._poll();
self.receiver(response.data);
break;
default:
this.poll();
}
}, },
_handlePollError: function(response, ioArgs) { _handleError: function(response, ioArgs) {
if (!this.polling) { console.log(response);
return;
}
this.polling = false;
this._poll();
} }
}); });
jocy.talk.connection = new jocy.talk.Connection();