0 Replies Latest reply on May 11, 2009 3:33 PM by mushion22

    Make socket events OO friendly?


      Hello there,


      I'm creating an application in Air using HTML/JS and need to use sockets. I want to put the socket within a class that will provide methods for interacting with the server via the socket. The application might connect to one or more servers, using a new instance of this class for each server.


      That works fine, except for event handling. Even when I create an instance of the class, and set the eventlistener for the socket in that instance to a function of that instance, it runs that function in the context of the Eventlistener (I think, or maybe the window) instead of that particular instance whose socket fired the event.


      So, I guess what I want to know is if there's a way to bind the event listener to fire the function (onSocketConnect / onSocketData) for a particular instance of a class or not? The alternative is to use a standard function that will call those methods on all the created objects, or perhaps there might be a fancy way to determine the correct object to call?


      I'm also using the prototype library to create my class. Here is my code thus far (class names changed and irrelevant code removed:



      var MyClass= Class.create({

      initialize: function(srvr,prt){

      this.server = srvr;

      this.port = prt;

      this.sock = new air.Socket();



      //some more fields








      connect: function(){

      if (debug)



      this.sock.connect(this.server, this.port);








      //function to handle what needs to happen after the socket is connected



      if (debug)

      air.trace("onSocketOpen " + un);



      //do some stuff




      //function to handle incoming message from the server


      onSocketData: function(event){

      //do some stuff






      //bunch of other functions







      // bind function for binding objects to function calls

      Function.prototype.bind = function(o, args){

          var fn = this;

          return function(){

              fn.apply(o, args || arguments);




      var AnotherClassObject= {

          theobject: new MyClass("server", 1234),


          init: function(){


              $('btnsend').onclick = AnotherClassObject.sendEvent.bind(Irkr);


              document.body.onmousedown = function(e){

                  if (e.target.tagName != 'INPUT')




              if (debug) air.trace("done the stuff");

      AnotherClassObject.theobject.sock.addEventListener(air.Event.CONNECT, AnotherClassObject.theobject.onSocketOpen);

      AnotherClassObject.theobject.sock.addEventListener(air.ProgressEvent.SOCKET_DATA, AnotherClassObject.theobject.onSocketData);



          AnotherClassObject. theobject.connect();



          sendEvent: function(evt){


              if (debug)


           //do something






      //helper function to get HTML elements

      function $(id){

          if (debug)

              air.trace("$(" + id + ")");

          return document.getElementById(id);


      //run the init function when the Air window loads


      window.onload = AnotherClassObject.init;




      Thanks a lot


      Message was edited by: mushion22: Typo