Skip to content

Commit

Permalink
v.0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos M committed May 10, 2016
1 parent 1e83d0e commit 0ddb1da
Show file tree
Hide file tree
Showing 18 changed files with 825 additions and 125 deletions.
103 changes: 64 additions & 39 deletions src/RT.BOSH.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,93 @@ RT.Client.BOSH = function Client_Bosh( cfg ) {
var self = this;
if ( !(self instanceof Client_Bosh) ) return new Client_Bosh(cfg);
__super__.constructor.call( self, cfg );
self.$xhr$ = null;
self.$cfg$.timeout = self.$cfg$.timeout || 1000;
self.$send$ = null;
self.$recv$ = null;
self.$mID$ = 0;
};
RT.Client.Impl['bosh'] = RT.Client.Impl['long-poll'] = RT.Client.BOSH;

/* extends RT.Client class */
RT.Client.BOSH[PROTO] = Object.create( __super__ );
RT.Client.BOSH[PROTO].constructor = RT.Client.BOSH;
RT.Client.BOSH[PROTO].$xhr$ = null;
RT.Client.BOSH[PROTO].$send$ = null;
RT.Client.BOSH[PROTO].$recv$ = null;
RT.Client.BOSH[PROTO].$mID$ = null;
RT.Client.BOSH[PROTO].dispose = function( ){
var self = this;
self.abort( );
self.$mID$ = null;
return __super__.dispose.call( self );
};
RT.Client.BOSH[PROTO].abort = function( trigger ){
var self = this;
if ( self.$xhr$ ) { self.$xhr$.abort( true===trigger ); self.$xhr$ = null; }
//if ( self.$send$ ) { self.$send$.abort( true===trigger ); self.$send$ = null; }
if ( self.$recv$ ) { self.$recv$.abort( true===trigger ); self.$recv$ = null; }
self.$send$ = null;
return self;
};
RT.Client.BOSH[PROTO].$poll$ = function( immediate ){
RT.Client.BOSH[PROTO].send = function( payload ){
var self = this;
var poll = function poll( ) {
var headers = {
XHR.create({
url : self.$cfg$.url + (-1 < self.$cfg$.url.indexOf('?') ? '&' : '?') + '__nocache__='+(new Date().getTime()),
method : 'POST',
responseType : 'text',
//mimeType : 'text/plain; charset=utf8',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=utf8',
'X-RT-Receive' : '1', // receive incoming message(s)
'X-RT-Timestamp' : self.$timestamp$
};
var rt_msg = null, msgs = null;
if ( self.$queue$.length )
{
// send message(s) on same request
headers['X-RT-Send'] = '1';
headers['X-RT-Message'] = rt_msg = RT.UUID('----------------------');
msgs = self.$queue$.slice( );
'X-RT--BOSH' : '1', // this uses BOSH
'X-RT--Send' : '1' // this is the send channel
},
onError : function( xhr ) {
self.emit('error', xhr.statusText);
},
onComplete : function( xhr ) {
var rt_close = xhr.responseHeader( 'X-RT--Close' ),
rt_error = xhr.responseHeader( 'X-RT--Error' )
;
if ( rt_error )
{
self.emit( 'error', rt_error );
return;
}
if ( rt_close )
{
self.close( );
return;
}
}
self.$xhr$ = XHR.create({
url : self.$cfg$.url + (-1 < self.$cfg$.url.indexOf('?') ? '&' : '?') + '__NOCACHE__='+(new Date().getTime()),
}, 'rt_payload='+U.Url.encode( String(payload) ));
return self;
};
RT.Client.BOSH[PROTO].listen = function( ){
var self = this;
self.emit( 'open' );
var listen = function listen( ) {
self.$recv$ = XHR.create({
url : self.$cfg$.url + (-1 < self.$cfg$.url.indexOf('?') ? '&' : '?') + '__nocache__='+(new Date().getTime()),
timeout : self.$cfg$.timeout,
method : 'POST',
responseType : 'text',
//mimeType : 'text/plain; charset=utf8',
headers : headers,
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=utf8',
'X-RT--BOSH' : '1', // this uses BOSH
'X-RT--Receive' : '1', // this is the receive channel
'X-RT--mID' : self.$mID$
},
onError : function( xhr ) {
self.emit('error', xhr.statusText);
self.$recv$ = null;
},
onTimeout : function( xhr ) {
self.$timer$ = setTimeout(poll, self.$cfg$.pollInterval);
setTimeout( listen, 0 );
},
onComplete : function( xhr ) {
var rt_msg = xhr.responseHeader( 'X-RT-Message' ),
rt_close = xhr.responseHeader( 'X-RT-Close' ),
rt_error = xhr.responseHeader( 'X-RT-Error' ),
rt_timestamp = xhr.responseHeader( 'X-RT-Timestamp' )
var rt_msg = xhr.responseHeader( 'X-RT--Message' ),
rt_close = xhr.responseHeader( 'X-RT--Close' ),
rt_error = xhr.responseHeader( 'X-RT--Error' ),
rt_mID = xhr.responseHeader( 'X-RT--mID' )
;
if ( rt_error )
{
Expand All @@ -93,25 +129,14 @@ RT.Client.BOSH[PROTO].$poll$ = function( immediate ){
for(i=0,l=msgs.length; i<l; i++)
self.emit('receive', msgs[i]);
}
if ( rt_timestamp ) self.$timestamp$ = rt_timestamp;
// message(s) sent
if ( msgs ) self.$queue$.splice( 0, msgs.length );
self.$timer$ = setTimeout(poll, self.$cfg$.pollInterval);
if ( rt_mID ) self.$mID$ = rt_mID;
setTimeout( listen, 0 );
}
}, msgs ? ('rt_payload='+U.Url.encode( msgs.join( rt_msg ) )) : null);
}, null);
};
self.$timer$ = setTimeout(poll, true === immediate ? 0 : self.$cfg$.pollInterval);
setTimeout( listen, 0 );
return self;
};
RT.Client.BOSH[PROTO].send = function( payload ){
var self = this;
self.$queue$.push( String(payload) );
return self;
};
RT.Client.BOSH[PROTO].listen = function( ){
var self = this;
return self.emit( 'open' ).$poll$( true );
};

// export it
return RT;
Expand Down
2 changes: 1 addition & 1 deletion src/RT.BOSH.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions src/RT.Poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RT.Client.Poll = function Client_Poll( cfg ) {
self.$cfg$.pollInterval = self.$cfg$.pollInterval || 1000;
self.$timer$ = null;
self.$xhr$ = null;
self.$timestamp$ = 0;
self.$mID$ = 0;
self.$queue$ = [];
};
RT.Client.Impl['poll'] = RT.Client.Impl['short-poll'] = RT.Client.Poll;
Expand All @@ -38,11 +38,11 @@ RT.Client.Poll[PROTO].constructor = RT.Client.Poll;
RT.Client.Poll[PROTO].$timer$ = null;
RT.Client.Poll[PROTO].$xhr$ = null;
RT.Client.Poll[PROTO].$queue$ = null;
RT.Client.Poll[PROTO].$timestamp$ = null;
RT.Client.Poll[PROTO].$mID$ = null;
RT.Client.Poll[PROTO].dispose = function( ){
var self = this;
self.abort( );
self.$timestamp$ = null;
self.$mID$ = null;
self.$queue$ = null;
return __super__.dispose.call( self );
};
Expand All @@ -57,19 +57,20 @@ RT.Client.Poll[PROTO].$poll$ = function( immediate ){
var poll = function poll( ) {
var headers = {
'Content-Type' : 'application/x-www-form-urlencoded; charset=utf8',
'X-RT-Receive' : '1', // receive incoming message(s)
'X-RT-Timestamp' : self.$timestamp$
'X-RT--Poll' : '1', // this uses polling
'X-RT--Receive' : '1', // receive incoming message(s)
'X-RT--mID' : self.$mID$
};
var rt_msg = null, msgs = null;
if ( self.$queue$.length )
{
// send message(s) on same request
headers['X-RT-Send'] = '1';
headers['X-RT-Message'] = rt_msg = RT.UUID('----------------------');
headers['X-RT--Send'] = '1';
headers['X-RT--Message'] = rt_msg = RT.UUID('----------------------');
msgs = self.$queue$.slice( );
}
self.$xhr$ = XHR.create({
url : self.$cfg$.url + (-1 < self.$cfg$.url.indexOf('?') ? '&' : '?') + '__NOCACHE__='+(new Date().getTime()),
url : self.$cfg$.url + (-1 < self.$cfg$.url.indexOf('?') ? '&' : '?') + '__nocache__='+(new Date().getTime()),
method : 'POST',
responseType : 'text',
//mimeType : 'text/plain; charset=utf8',
Expand All @@ -81,10 +82,10 @@ RT.Client.Poll[PROTO].$poll$ = function( immediate ){
self.$timer$ = setTimeout(poll, self.$cfg$.pollInterval);
},
onComplete : function( xhr ) {
var rt_msg = xhr.responseHeader( 'X-RT-Message' ),
rt_close = xhr.responseHeader( 'X-RT-Close' ),
rt_error = xhr.responseHeader( 'X-RT-Error' ),
rt_timestamp = xhr.responseHeader( 'X-RT-Timestamp' )
var rt_msg = xhr.responseHeader( 'X-RT--Message' ),
rt_close = xhr.responseHeader( 'X-RT--Close' ),
rt_error = xhr.responseHeader( 'X-RT--Error' ),
rt_mID = xhr.responseHeader( 'X-RT--mID' )
;
if ( rt_error )
{
Expand All @@ -103,7 +104,7 @@ RT.Client.Poll[PROTO].$poll$ = function( immediate ){
for(i=0,l=msgs.length; i<l; i++)
self.emit('receive', msgs[i]);
}
if ( rt_timestamp ) self.$timestamp$ = rt_timestamp;
if ( rt_mID ) self.$mID$ = rt_mID;
// message(s) sent
if ( msgs ) self.$queue$.splice( 0, msgs.length );
self.$timer$ = setTimeout(poll, self.$cfg$.pollInterval);
Expand Down
2 changes: 1 addition & 1 deletion src/RT.Poll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/RT.WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ RT.Client.WS[PROTO].listen = function( ){
ws.addEventListener('error', function( e ) {
self.emit('error', e);
});
ws.addEventListener('message', function( evt ) {
ws.addEventListener('message', function( e ) {
self.emit('receive', e.data);
});
return self;
};

// export it
Expand Down
Loading

0 comments on commit 0ddb1da

Please sign in to comment.