Skip to content

Commit

Permalink
v.1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos M committed May 11, 2016
1 parent 273c29b commit 4cbf0b0
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 223 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Unified Node/XPCOM/JS client-side real-time communication with underlying implem
* [WebSocket](https://en.wikipedia.org/wiki/WebSocket)


**Note** `RT` is not only a simple framework around real-time layer implementations, it is also a small protocol additional to an implementation, which enables optimum performance, e.g by multiplexing multiple requests transparently (where applicable).


**Example API**

Expand Down Expand Up @@ -48,7 +50,7 @@ var rt_chat = RT({
<span class="message">' + textify( m.message ) + '</span>\
</div>';
})
/*.on('open', function( ){
/*.one('open', function( ){
alert('OPENED');
})*/
.on('close', function( ){
Expand Down
37 changes: 24 additions & 13 deletions src/RT.BOSH.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ RT.Client.BOSH[PROTO].$queue$ = null;
RT.Client.BOSH[PROTO].$mID$ = null;
RT.Client.BOSH[PROTO].dispose = function( ){
var self = this;
if ( self.$bosh$ ) { self.$bosh$.abort( false ); self.$bosh$ = null; }
if ( self.$bosh$ ) { self.$bosh$.abort( ); self.$bosh$ = null; }
self.$queue$ = null;
self.$mID$ = null;
return __super__.dispose.call( self );
};
RT.Client.BOSH[PROTO].abort = function( trigger ){
var self = this;
if ( self.$bosh$ ) { self.$bosh$.abort( true===trigger ); self.$bosh$ = null; }
if ( self.$bosh$ ) { self.$bosh$.abort( ); self.$bosh$ = null; }
return __super__.abort.call( self, true===trigger );
};
RT.Client.BOSH[PROTO].send = function( payload ){
Expand All @@ -58,6 +58,7 @@ RT.Client.BOSH[PROTO].listen = function( ){
var self = this;
var listen = function listen( ) {
var headers = {
'Connection' : 'Keep-Alive',
'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
Expand All @@ -79,36 +80,46 @@ RT.Client.BOSH[PROTO].listen = function( ){
//mimeType : 'text/plain; charset=utf8',
headers : headers,
onError : function( xhr ) {
self.emit('error', xhr.statusText);
self.$bosh$ = null;
self.emit( 'error', xhr.statusText );
},
onTimeout : function( xhr ) {
self.$bosh$ = null;
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_mID = xhr.responseHeader( 'X-RT--mID' )
var rt_msg = xhr.getResponseHeader( 'X-RT--Message' ),
rt_close = xhr.getResponseHeader( 'X-RT--Close' ),
rt_error = xhr.getResponseHeader( 'X-RT--Error' ),
rt_mID = xhr.getResponseHeader( 'X-RT--mID' )
;
if ( rt_error )
{
self.$bosh$ = null;
return self.emit( 'error', rt_error );
}
if ( rt_close )
{
self.$bosh$ = null;
return self.close( );
}

if ( rt_mID ) self.$mID$ = rt_mID;
// message(s) sent
if ( msgs ) self.$queue$.splice( 0, msgs.length );

if ( rt_msg )
{
// at the same time, handle incoming message(s)
var msgs = (xhr.responseText||'').split( rt_msg ), i, l;
for(i=0,l=msgs.length; i<l; i++)
self.emit('receive', msgs[i]);
var received = (xhr.responseText||'').split( rt_msg ), i, l;
for(i=0,l=received.length; i<l; i++) self.emit( 'receive', received[i] );
}
if ( rt_mID ) self.$mID$ = rt_mID;
// message(s) sent
if ( msgs ) self.$queue$.splice( 0, msgs.length );
else if ( !!xhr.responseText )
{
self.emit( 'receive', xhr.responseText );
}

self.$bosh$ = null;
setTimeout( listen, 0 );
}
}, msgs ? ('x-rt--payload='+U.Url.encode( msgs.join( rt_msg ) )) : null);
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.

36 changes: 24 additions & 12 deletions src/RT.Poll.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ RT.Client.Poll[PROTO].$mID$ = null;
RT.Client.Poll[PROTO].dispose = function( ){
var self = this;
if ( self.$timer$ ) { clearTimeout( self.$timer$ ); self.$timer$ = null; }
if ( self.$xhr$ ) { self.$xhr$.abort( false ); self.$xhr$ = null; }
if ( self.$xhr$ ) { self.$xhr$.abort( ); self.$xhr$ = null; }
self.$mID$ = null;
self.$queue$ = null;
return __super__.dispose.call( self );
};
RT.Client.Poll[PROTO].abort = function( trigger ){
var self = this;
if ( self.$timer$ ) { clearTimeout( self.$timer$ ); self.$timer$ = null; }
if ( self.$xhr$ ) { self.$xhr$.abort( true===trigger ); self.$xhr$ = null; }
if ( self.$xhr$ ) { self.$xhr$.abort( ); self.$xhr$ = null; }
return __super__.abort.call( self, true===trigger );
};
RT.Client.Poll[PROTO].send = function( payload ){
Expand Down Expand Up @@ -82,35 +82,47 @@ RT.Client.Poll[PROTO].listen = function( ){
//mimeType : 'text/plain; charset=utf8',
headers : headers,
onError : function( xhr ) {
self.$xhr$ = null;
self.emit( 'error', xhr.statusText );
},
onTimeout : function( xhr ) {
self.$xhr$ = null;
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_mID = xhr.responseHeader( 'X-RT--mID' )
var rt_msg = xhr.getResponseHeader( 'X-RT--Message' ),
rt_close = xhr.getResponseHeader( 'X-RT--Close' ),
rt_error = xhr.getResponseHeader( 'X-RT--Error' ),
rt_mID = xhr.getResponseHeader( 'X-RT--mID' )
;
if ( rt_error )
{
self.$xhr$ = null;
return self.emit( 'error', rt_error );
}
if ( rt_close )
{
self.$xhr$ = null;
return self.close( );
}

if ( rt_mID ) self.$mID$ = rt_mID;
// message(s) sent
if ( msgs ) self.$queue$.splice( 0, msgs.length );

if ( rt_msg )
{
// at the same time, handle incoming message(s)
var msgs = (xhr.responseText||'').split( rt_msg ), i, l;
for(i=0,l=msgs.length; i<l; i++) self.emit( 'receive', msgs[i] );
var received = (xhr.responseText||'').split( rt_msg ), i, l;
for(i=0,l=received.length; i<l; i++) self.emit( 'receive', received[i] );
}
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);
else if ( !!xhr.responseText )
{
self.emit( 'receive', xhr.responseText );
}

self.$xhr$ = null;
self.$timer$ = setTimeout( poll, self.$cfg$.pollInterval );
}
}, msgs ? ('x-rt--payload='+U.Url.encode( msgs.join( rt_msg ) )) : null);
};
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.

Loading

0 comments on commit 4cbf0b0

Please sign in to comment.