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)
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;
Thanks a lot
Message was edited by: mushion22: Typo

int ct = read.selectNow();
if (ct > 0)
Set readyKeys = read.selectedKeys();
Iterator i = readyKeys.iterator();
while (i.hasNext())
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isReadable())
SelectableChannel nextReady = (SelectableChannel) key.channel();
SocketChannel sc = (SocketChannel) key.channel();
int rd = sc.read(buffer);
// Link is dead
if (rd == -1)
key.cancel();
key.attach(null);
nextReady.close();
continue;
// Process your data
catch (Exception e)
e.printStackTrace();

Similar Messages

  • Socket event gateway error in CF8?

    Having upgraded to CF8 with no code changes I am seeing a
    socket event gateway accept an xml message line by line and not the
    entire xml packet in one shot (each line is shown in the log
    separately). Passing a simple sentence with a return in it also
    becomes 2 events. This obviously causes problems, as the xml cannot
    be recognized and processed when it comes in line by line. The xml
    is coming from a daemon writen in Perl, and I can't immediately
    make any changes to it, as these requests are also sent out to
    other Perl systems, so any changes would have to be tested against
    them as well.
    I couldn't find anything online about this being a bug in
    CF8, has anyone else seen it?

    Hi,
    Remove this (And never put it in again)
    EXCEPTION
    when others then
    raise_application_error(-20001,'Error sending email.Error: '||sqlerrm);
    --Dbms_Put.Put_Line('Error sending email.Error: '||dbms_util.get_detailed_sqlerrm);This will tell you more about what is going on, such as giving you the location of the error.
    But 20999, is a user defined exception, are you putting objects into system schema?
    Regards
    Peter

  • How do I make iPhoto events with photo stream?

    I am confused about how I am now supposed to download photos into iphoto to make Events.  Yesterday I plugged in my phone to import recent photos but when it did this I ended up with two of everything because photostream had already "imported" (?) them.  So, if that means I don't need to plug the iphone in pto import the photos, then how do I:
    1.  delete the photos from my phone that I would normally be deleting everytime i import via a wire?
    2.  make an event from the photos in the photo stream?
    I also noticed that some of the photos from the same series of photos are still left on my phone--those are the only photos that are not duplicated.  I don't understand why they were left on my phone, nor is it clear to me whether the ones on my computer are from the "normal import" or from the "photo stream import". 
    Would love some clarity about this.

    As far as the duplicates.... I don't know why it's doing that. If something comes to mind I'll let you know.
    As far as using a cable...
    You can still sync with iTunes (apps, music, photos etc) via Wi-Fi and only use the cable to charge the phone. That has nothing to do with Photostream. So you should have iTunes set up to sync the photos with a folder on your MBP.
    As far as being away from your MBP for over 30 days....
    Well, in that case whatever photos are older than 30 days will disappear from photostream, but they will still be in your phone's camera roll. As long as you don't lose or break your phone, you can transfer them to your MBP when you're home again (via iTunes or iPhoto or Image Capture). If you are concerned something will happen to your phone during those 30+ days away from your MBP, then you can use an option like Dropbox to save them online.

  • HT4890 Can I share a specific event with friends on iCloud via Photostream?

    Can I share a specific event with friends on iCloud via Photostream?

    No, not until iOS 6 comes out.

  • Is it possible to make an event that does not have an end time?

    I want to make an event that says I have a meeting at 4:30pm. I don't know when they meeting will end, so how do I leave that blank?

    Hi Bala, you are right, the user must be able to give discounts to items that can be discounted. Most often the discounts will come from the BP price list and associated special and hierarchic pricing.
    As Rakesh suggested, I shall run it by our client if they would want to limit discount ability across the board for their users through General authorizations and perhaps allow for only some users who could override manual discounts to certain limits. Again it pertain to the ability to override manual discounts on top of BP discounts through list for all items for some users only.
    Thanks again Bala and Rakesh.
    Edited by: Syed Aleem on Jan 11, 2011 1:32 AM

  • HT1692 not able to sync exchange calendar events on iPad made from my computer or iPhone. It works fine when I make an event from iPad, it shows on computer and iPhone but not vice-versa.

    I have an iPhone, Ipad and a Laptop. They all are configured to company's exch.I am not able to sync calendar events on iPad made from my computer or iPhone. It works fine when I make an event from iPad, it shows on computer and iPhone but not vice-versa.

    -Me again - it also won't upload the few new pictures I have on the computer onto the iPad without deleting all the photos that are already there....

  • Can I make sub-events in iPhoto? Can I delete originals of imported photos?

    1) Is there any way I can make sub-events? I have pictures of family members, grouped by one event per member. Is it possible to have a Family event, and then sub events for each member? Is this better addressed through albums?
    2) When I download an image from the web to my hard drive and later import it to iphoto, can I delete the originally saved file without affecting the image's place in iphoto?
    Thank you for your help,
    Conrad

    schmorrell wrote:
    1) Is there any way I can make sub-events? I have pictures of family members, grouped by one event per member. Is it possible to have a Family event, and then sub events for each member?
    No - there are no sub events - events are time based not subject based
    Is this better addressed through albums?
    Yes - or keywords and smart albums - what you want is a family folder with albums (smart or otherwise) for each member
    2) When I download an image from the web to my hard drive and later import it to iphoto, can I delete the originally saved file without affecting the image's place in iphoto?
    Yes - assuming that you are using the default (and highly recommended) "copy items to the iPhoto library" preference being checked (if it is not then you can not delete the "original")
    It is not a bad idea to back up your originals and also you should make regular backups of the iPhoto library - and use Old Toad's library6.iPhoto database file backup very often while you are loading and editing photos
    LN

  • HT4759 How can make the events listed in my chalenda become automatically a reminder in the reminder?:

    How can make the events listed in my chalenda become automatically a reminder in the reminder?:

    Welcome to the Apple community.
    Calendar and reminders are two different and separate apps, you cannot transfer information between the two of them.

  • Movieclip movement triggered by socket event, need more frequent rendering

    I have a flash project that's implementing a simple two player game.  I have added an enterFrame function to my main movie which listens for keyboard activity in order to move a cowboy gunslinger.  Whenever the cowboy moves, I send a message over a socket to a server which dispatches the movement to the other connected client.  This works pretty well, however, the movement messages tend to arrive in clumps which results in jumpy animation of my cowboy on my opponents screen and jumpy animation of his cowboy on my screen.  To illustrate, i put a trace in my enterFrame function because I am imagining that this function will run roughly once each time my main movie renders the screen.  I also put a trace statement in my function that responds to network traffic and moves the remote player's cowboy on my screen.  Here's the output:
    enter frame:4
    enter frame:4
    enter frame:4
    client movePlayer running
    client movePlayer running
    client movePlayer running
    client movePlayer running
    client movePlayer running
    enter frame:4
    enter frame:4
    enter frame:4
    enter frame:4
    enter frame:4
    enter frame:4
    client movePlayer running
    client movePlayer running
    client movePlayer running
    client movePlayer running
    client movePlayer running
    client movePlayer running
    enter frame:4
    enter frame:4
    enter frame:4
    enter frame:4
    As you can see, I'll get several messages in a row from the remote client which instruct me to move their cowboy ('client MovePlayer running') and they will all run in a row before the screen updates.
    I'm guessing I should be using something like updateAfterEvent but this method is only provided by Mouse, Timer, and Keyboard events.
    So a few questions:
    1) Can someone recommend a good approach to force a screen render each time an incoming movePlayer event arrives over the socket?  It's important to note that my function for handling these events has no visibility to the original socket data event.
    2) Am I right in understanding that the enterFrame function of my main movie happens once each time my movie is rendered?  Is there some more accurate event to which I could attach a trace message so that I better understand the relative frequency of the render events and the socket events?
    3) Does firing an updateAfterEvent call force onEnterFrame events to happen more frequently?  I'm worried about destroying performance by inadvertently firing more enterFrames which would fire more socket events which would fire more enterFrames, etc., etc.
    Any help would be much appreciated.

    I have noticed that while my server appears to be writing bytes on a regular basis, roughly every 30 or 40 milliseconds:
    51 bytes sent at 1242146737.69
    43 bytes sent at 1242146737.72
    43 bytes sent at 1242146737.76
    43 bytes sent at 1242146737.79
    43 bytes sent at 1242146737.82
    43 bytes sent at 1242146737.86
    43 bytes sent at 1242146737.89
    43 bytes sent at 1242146737.92
    52 bytes sent at 1242146737.96
    51 bytes sent at 1242146739.46
    43 bytes sent at 1242146739.49
    44 bytes sent at 1242146739.52
    44 bytes sent at 1242146739.56
    44 bytes sent at 1242146739.59
    44 bytes sent at 1242146739.62
    44 bytes sent at 1242146739.66
    53 bytes sent at 1242146739.69
    the socketData event is only firing every 150-200 milliseconds and these packages arrive in clumps:
    * socketDataHandler running at 1242153515.5
    client.movePlayer running at 1242153515.515
    * socketDataHandler running at 1242153515.703
    client.movePlayer running at 1242153515.703
    client.movePlayer running at 1242153515.703
    client.movePlayer running at 1242153515.703
    client.movePlayer running at 1242153515.703
    client.movePlayer running at 1242153515.703
    * socketDataHandler running at 1242153515.89
    client.movePlayer running at 1242153515.89
    client.movePlayer running at 1242153515.906
    client.movePlayer running at 1242153515.906
    client.movePlayer running at 1242153515.906
    client.movePlayer running at 1242153515.906
    client.movePlayer running at 1242153515.906
    * socketDataHandler running at 1242153516.093
    client.movePlayer running at 1242153516.109
    client.movePlayer running at 1242153516.109
    client.movePlayer running at 1242153516.109
    client.movePlayer running at 1242153516.109
    client.movePlayer running at 1242153516.109

  • What computer manufacturer makes the most "linux friendly" laptops?

    Hi,
    I'm about to get a new laptop to replace my Dell E1505 I used untill now. i'm very happy with it, but it got old and starts to fail. I want to get a new laptop that's simular to it but new and more up to date. I looked at some of the computers avaliable and was thinking, which manufacturer makes the most linux friendly computers? Should I stick with dell or maybe HP is the better choice? What's your experiance on the subject?

    From what I've seen, major manufacturers give little-to-no concern towards Linux support when designing a product.  Should they have a string of products which work hassle-free, it is through coincidence alone.
    To take Lenovo as an example, many of their notebooks in the past have included a wireless card they referred to as a "ThinkPad wireless adapter", which was renowned for functioning 'out of the box' with the ath5k driver.  During the middle(?) of last year, and without indication, the "ThinkPad wireless adapters" changed, and now use an rtl8192se chipset.  ndiswrapper has limited success (if any); a functional driver has only recently been created for this chipset; and support still missing from the kernel.
    While my next laptop will probably be a Lenovo it will be for their build-quality and not their Linux support.  When looking for a laptop, I recommend finding something which matches your specifications, design and price and then thoroughly checking compatibility of the components: it's the only way to be sure.

  • "Make new event" greyed out

    Two questions:
    After selecting photos, I cannot get "make new event" to appear as anything other than greyed out and inaccessible. How do I get tihs to be available?
    Second question: Is there a way of hiding photos in the library that does not hide them after they have been sorted into albums or events etc?
    Thanks for help. The transition to Mac from IBM can be painful, but as we all know, "Resistance is futile."

    It is a sort of gmail account.
    It is Virginmedia email which is run by gmail.
    I have given up on Google things apart from the basic search. They have made things so complicated I have no idea what is happening and you can never contact anyone to sort it out and I have found their forums useless.
    I used most of the products they have bought before hand and I ended up with about 6 google accounts. I managed to get it down to about 3 but now I avoid it if at all possible.
    I don't think virginmedia really know how the system works.
    I have 2 email adresses I use and eventually I got the first one to work ok on Thunderbird nut I had a lot of trouble getting the other to work and then suddenly it appeared on Thunderbird, I still have no idea how it happened but it works in a completely different way to the first address.

  • How do you make a event reoccur on the last weekday of every month?

    How do you make a event reoccur on the last weekday of every month in lightning?
    This can be done in outlook and lightning accepts and properly handles the event from a outlook user.

    Hi,
    It seems it cannot be done. You can add your vote to this bug report if you want to keep informed:
    [https://bugzilla.mozilla.org/show_bug.cgi?id=390620 Bug 390620 - Event / Task recurrence options for "Last Working Day" and "First Working Day" of the month]

  • I have Photoshop CC and I'd like to make an event flyer.  Are there any tutorials, templates, help???

    I have Photoshop CC and I'd like to make an event flyer.  Are there any tutorials, templates, help???

    Since your question is about Photoshop, I've moved it to the Photoshop General forum.

  • How to make fake events on JButton?

    Hi ,
    how can i make fake events on JButton.
    Like when i click on the button A i want
    button B has to pressed and the app. listeners
    has to be called.
    thanks in advance
    Johinth

    Visit JDC Tech Tip page at:
    http://developer.java.sun.com/developer/JDCTechTips/
    A topic on Automating GUI Programs with java.awt.Robot could be helpful to you.

  • Easy way to make my site CSS friendly without changing everything??

    I want to make my site CSS friendly but based on my research on the internet seems to be really tricky to do and time consuming.  Do I just take out all of the pictures and just have text and save it as an alternative?  What do I do to make my site "smart" so to speak and recognize if someone's computer needs CSS or not? 

    The main issue with the website as it stands is that there's no text on the home page.
    If you offered a text-only version for slower connections (which I would not worry about), there would be nothing to show.
    I would forget about CSS for now (there's plenty of auto-generated CSS already in the code BTW) and focus on these Google guidelines:
    http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=35769
    I see allot of web sites these days that have that orange CSS in the address bar and I know it must be important somehow and wish to learn about it.  Maybe I'm getting CSS mixed up in what I'm thinking.  Not sure.
    I think you're getting the acronyms mixed up.
    That orange icon is an indicator of an RSS (Really Simple Syndication) feed available on the website, not CSS. It's known as a Live Bookmark in Firefox and allows you to stay in touch with sites without visiting them all the time.
    http://www.mozilla.com/en-US/firefox/livebookmarks.html
    http://johnbokma.com/firefox/rss-and-live-bookmarks.html

Maybe you are looking for

  • 4Th Generation Time Capsule Running Backup very Slow

    I am having a problem with my 4th generation time capsule as it is running very slow , it shows that i need to backup 151 Gb data and for the last 2 days the device backup only 150 mb, pls advise what is the problem? setup of the wireless network see

  • Printer to be used in DSD application

    Hi, My client is using HP printers right now with an legacy application instaled on Palm Devices. They print using Bluetooth. We are developing MDSD application for them and want to know about the devices which we can use. We are at MI 7.0 sp16. Will

  • Adobe Form in HRFORMS: Value of Textfield on Masterpage is not rendered!

    Hi all, I design a new form using HRFORMS. When I put a simple textfield on a master page and give the textfield a default value and render my form using transaction PC00_M01_CALC_SIMU, the value of the textfield is NOT rendered. I am using Adobe Liv

  • All i want is a ******* working ipod!

    Hi, I've posted on here before about various technical issues with ipods and now I basically find myself lumbered with two that don't work and after all the expense I can't bear to go down the road of shelling out more extortionate amounts to have co

  • Ipod notes & text file splitting

    Does my Ipod support the HTML code in my txt files? How I can split in txt's file of 4 kb my e-book?Is there any program to do that?Is there any special function using the ipod notes?Thank you.