Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


mushion22

297 posts

Ultimate Geek

Trusted

#33787 12-May-2009 10:47
Send private message

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) air.trace("connect");


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


},


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


onSocketOpen:function(event){


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')


                nativeWindow.startMove();


        }


        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)


            air.trace("sendEvent");


     //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;




Create new topic
magu
Professional yak shaver
1599 posts

Uber Geek
+1 received by user: 7

Trusted
BitSignal
Lifetime subscriber

  #214316 12-May-2009 11:22
Send private message

Can't you pass the socket handle to the function?

For reference, it's best to paste code like this on pastebin.com or paste2.org.




"Roads? Where we're going, we don't need roads." - Doc Emmet Brown



mushion22

297 posts

Ultimate Geek

Trusted

  #214317 12-May-2009 11:23
Send private message

A friend has suggested i change the event listener things to:



AnotherClassObject.theobject.sock.addEventListener(air.Event.CONNECT, function(event) {

AnotherClassObject.theobject.onSocketOpen();

});





Will have to try later when i get home.

mushion22

297 posts

Ultimate Geek

Trusted

  #214323 12-May-2009 11:30
Send private message

Actually that doesn't help me that much. Still have to hard cost all the EventListeners for every instance of the object. Need a way to do it dynamically.



magu
Professional yak shaver
1599 posts

Uber Geek
+1 received by user: 7

Trusted
BitSignal
Lifetime subscriber

  #214324 12-May-2009 11:30
Send private message

I have done something similar on a project a while ago, and it did work fine. Didn't involve sockets, though, so I don't know if it applies.




"Roads? Where we're going, we don't need roads." - Doc Emmet Brown

Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.