43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
/*
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
* Copyright (c) 2011-2012 VMware, Inc.
|
|
*
|
|
* For the license see COPYING.
|
|
* ***** END LICENSE BLOCK *****
|
|
*/
|
|
|
|
var XhrReceiver = function(url, AjaxObject) {
|
|
var that = this;
|
|
var buf_pos = 0;
|
|
|
|
that.xo = new AjaxObject('POST', url, null);
|
|
that.xo.onchunk = function(status, text) {
|
|
if (status !== 200) return;
|
|
while (1) {
|
|
var buf = text.slice(buf_pos);
|
|
var p = buf.indexOf('\n');
|
|
if (p === -1) break;
|
|
buf_pos += p+1;
|
|
var msg = buf.slice(0, p);
|
|
that.dispatchEvent(new SimpleEvent('message', {data: msg}));
|
|
}
|
|
};
|
|
that.xo.onfinish = function(status, text) {
|
|
that.xo.onchunk(status, text);
|
|
that.xo = null;
|
|
var reason = status === 200 ? 'network' : 'permanent';
|
|
that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
|
|
}
|
|
};
|
|
|
|
XhrReceiver.prototype = new REventTarget();
|
|
|
|
XhrReceiver.prototype.abort = function() {
|
|
var that = this;
|
|
if (that.xo) {
|
|
that.xo.close();
|
|
that.dispatchEvent(new SimpleEvent('close', {reason: 'user'}));
|
|
that.xo = null;
|
|
}
|
|
};
|