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

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

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





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









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.