cybertools/ajax/jocy/talk.js
helmutm 057d498fd4 work in progress: communication client using long polling
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3719 fd906abe-77d9-0310-91a1-e0d9ade77398
2010-02-08 14:20:39 +00:00

124 lines
3.1 KiB
JavaScript

/*
* Copyright (c) 2010 Helmut Merz helmutm@cy55.de
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// $Id$
dojo.provide('jocy.talk');
dojo.declare('jocy.talk.Connection', null, {
info: 'jocy.talk Connection',
constructor: function() {
this.receiver = null;
this.polling = false;
this.id = null;
},
open: function(receiver, id) {
this.receiver = receiver;
if (id != undefined) {
this.id = id;
} else {
this._getid();
}
this._poll();
return this.id;
},
close: function() {
return dojo.xhrPost({
url: '/.stop/' + this.id,
load: dojo.hitch(this, function(response, ioArgs) {
this.polling = false;
})
});
},
send: function(path, data) {
return dojo.xhrPost({
url: '/.send/' + this.id + path,
postData: dojo.toJson(data)
});
},
// private methods
_getId: function() {
return dojo.xhrPost({
url: '/.getid',
sync: true,
load: dojo.hitch(this, function(response, ioArgs) {
this.id = response;
})
});
},
_poll: function() {
if (this.polling) {
return;
}
if (this.id == undefined) {
this._getId();
}
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) {
if (!this.polling) {
return;
}
this.polling = false;
t = response.type;
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) {
if (!this.polling) {
return;
}
this.polling = false;
this._poll();
}
});
jocy.talk.connection = new jocy.talk.Connection();