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
This commit is contained in:
parent
a223ff070a
commit
057d498fd4
1 changed files with 74 additions and 60 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2009 Helmut Merz helmutm@cy55.de
|
* Copyright (c) 2010 Helmut Merz helmutm@cy55.de
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -23,88 +23,102 @@
|
||||||
dojo.provide('jocy.talk');
|
dojo.provide('jocy.talk');
|
||||||
|
|
||||||
|
|
||||||
dojo.declare('jocy.talk.Manager', null, {
|
|
||||||
|
|
||||||
info: 'jocy.talk Manager',
|
|
||||||
|
|
||||||
constructor: function(){
|
|
||||||
this.connections = {}; // map URLs to connection objects
|
|
||||||
},
|
|
||||||
|
|
||||||
start: function(url, receiver, user, password) {
|
|
||||||
url = url.replace(/\/$/, '');
|
|
||||||
// TODO: close connection if already present
|
|
||||||
var conn = new jocy.talk.Connection(url, receiver, user, password);
|
|
||||||
this.connections[url] = conn;
|
|
||||||
conn.init();
|
|
||||||
conn.start();
|
|
||||||
},
|
|
||||||
|
|
||||||
send: function(url, data) {
|
|
||||||
},
|
|
||||||
|
|
||||||
close: function(url) {
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
jocy.talk.manager = new jocy.talk.Manager();
|
|
||||||
|
|
||||||
|
|
||||||
dojo.declare('jocy.talk.Connection', null, {
|
dojo.declare('jocy.talk.Connection', null, {
|
||||||
|
|
||||||
info: 'jocy.talk Connection',
|
info: 'jocy.talk Connection',
|
||||||
|
|
||||||
constructor: function(url, receiver, user, password) {
|
constructor: function() {
|
||||||
this.url = url.replace(/\/$/, '');
|
this.receiver = null;
|
||||||
this.receiver = receiver; // receive callback function
|
this.polling = false;
|
||||||
this.user = user;
|
this.id = null;
|
||||||
this.password = password;
|
|
||||||
this.initialized = false;
|
|
||||||
this.clientId = ''; // id provided by server
|
|
||||||
},
|
},
|
||||||
|
|
||||||
init: function() {
|
open: function(receiver, id) {
|
||||||
// request client id
|
this.receiver = receiver;
|
||||||
message = {request: 'start'};
|
if (id != undefined) {
|
||||||
|
this.id = id;
|
||||||
|
} else {
|
||||||
|
this._getid();
|
||||||
|
}
|
||||||
|
this._poll();
|
||||||
|
return this.id;
|
||||||
|
},
|
||||||
|
|
||||||
|
close: function() {
|
||||||
return dojo.xhrPost({
|
return dojo.xhrPost({
|
||||||
url: this.url,
|
url: '/.stop/' + this.id,
|
||||||
postData: dojo.toJson(message),
|
|
||||||
handleAs: 'json',
|
|
||||||
load: dojo.hitch(this, function(response, ioArgs) {
|
load: dojo.hitch(this, function(response, ioArgs) {
|
||||||
this.clientId = response.client_id;
|
this.polling = false;
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
start: function() {
|
send: function(path, data) {
|
||||||
// start polling
|
|
||||||
},
|
|
||||||
|
|
||||||
close: function() {
|
|
||||||
},
|
|
||||||
|
|
||||||
send: function(resourcePath, data) {
|
|
||||||
return dojo.xhrPost({
|
return dojo.xhrPost({
|
||||||
url: this._setupUrl(resourcePath),
|
url: '/.send/' + this.id + path,
|
||||||
load: dojo.hitch(this, function(response, ioArgs) {
|
|
||||||
return this._callback(response, ioArgs);
|
|
||||||
}),
|
|
||||||
postData: dojo.toJson(data)
|
postData: dojo.toJson(data)
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// private methods
|
// private methods
|
||||||
|
|
||||||
|
_getId: function() {
|
||||||
|
return dojo.xhrPost({
|
||||||
|
url: '/.getid',
|
||||||
|
sync: true,
|
||||||
|
load: dojo.hitch(this, function(response, ioArgs) {
|
||||||
|
this.id = response;
|
||||||
|
})
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
_poll: function() {
|
_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)
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
_setupUrl: function(path) {
|
_handlePollResponse: function(response, ioArgs) {
|
||||||
slash = (path.charAt(0) == '/' ? '' : '/');
|
if (!this.polling) {
|
||||||
return this.url + slash + path;
|
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();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_alert: function(data) {
|
_handlePollError: function(response, ioArgs) {
|
||||||
alert(data);
|
if (!this.polling) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.polling = false;
|
||||||
|
this._poll();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
jocy.talk.connection = new jocy.talk.Connection();
|
||||||
|
|
Loading…
Add table
Reference in a new issue