views:

14

answers:

1

I am have a problem with creating a server push network object. Because Firefox and chrome handle server push differently, (Firefox get onload events, chrome uses onprogress /broken ). For chrome I need to capture the onprogress events and then mask off the previous data from the responseText. I want to pass just the new data to chrome.

in sendpush() there are two functions the I use to set the even handler.

I know this has something to do with closure and scope,

Any ides?

function prog( evt, obj, func ) {
 var r = evt.target;
 var t = typeof( r.responseText);
 var z = "test".subStr( 2 );
 var d = r.responseText.subString( obj.oldLen );
 var s = {};
 s["code"] = r.status;
 s["text"] = r.statusText;
 func( s, d );
};

 function maRequest( url, method, multi ) {
 this.method = "POST";
 this.multi = false;
 this.url = url;
 this.self = this;
 this.oldLen = 0;

 if ( method != undefined )
  this.method = method;

 if ( multi != undefined )
  this.multi = multi;

 var req = new XMLHttpRequest();
 this._req = req;
 req.multipart = this.multi;

/* this._req.onreadystatechange = function( evt ) {*/
 this.send = function( data, loadcb ) {
  this._req.onload = function( evt ) {
   var r = evt.target;
   var stat = {};
   stat["code"] = r.status;
   stat["text"] = r.statusText;
   loadcb( stat, r.responseText );
   return false;
  };
  this._req.open( this.method, this.url, true );
  try {
   this._req.send( data );
  } catch (e) {
   alert( e );
  }
 }; 

 this.setProgress = function( func ) {
  var self = this;
  self._req.onprogress = function ( evt ) {
   return prog( evt, self, func );
  } 
 }; 

 this.addCallback = function( name, func ) {
  var self = this;
  var cb = function ( evt ) {
   func( evt, self );
  }
  switch( name ) { 
  case "loadstart":
   req.onloadstart  = cb;
   break;
  case "progress":
   req.onprogress  = cb;
   break;
  case "abort":
   req.onabort  = cb;
   break;
  case "error":
   req.onerror  = cb;
   break;
  default:
   req.addEventListener( name, function ( evt ) {
    func( evt, self );
   }, false);
  };
 };

 this.abort = function() {
  req.abort();
 };
};

// works when passed as callback function
function progress( evt, obj) {
 var req = evt.target;
 stat = req.status;
 alert( req.statusText+" "+req.readyState+" "+ req.responseText.substr( obj.oldLen) );
 obj.oldLen = req.responseText.length;
 return true;
}

function maChat() {
 this.url = "/mafw/chat.ma";
}
maChat.prototype = new maRequest( this.url );

function callback( status, data ) {
 alert( status["code"]+status["text"]+" "+data );
}

function sendmsg( ) {
 var cmd = {};
 cmd["type"] = "cmd";
 cmd["cmd"] = "initdata";
 cmd["me"] = 0;

// try {
//  var conn = new maRequest( "http://localhost/mafw/chat.ma" );
//  conn.send( JSON.stringify ( cmd ), callback );
// } catch (e ) {
//  alert (e);
// }
 try {
  var conn1 = new maChat();
  conn1.send( JSON.stringify ( cmd ), callback );
 } catch (e ) {
  alert (e);
 }
};


function sendpush() {
 var cmd = {};
 cmd["type"] = "cmd";
 cmd["cmd"] = "initdata";
 cmd["me"] = 0;

 try {
  var conn = new maRequest( "http://localhost/mafw/chat.psh", "POST", true );
  //conn.addCallback(  "progress", progress );  
  conn.setProgress( callback );
  conn.send( JSON.stringify ( cmd ), callback );
 } catch (e ) {
  alert (e);
 }
}
+1  A: 

It's in lowercase, use substr, not subStr. The same goes for substring, it should be lowercase too.

Lekensteyn