Connecting to Socket.IO 1.0 from flash

Socket.IO is a great socket communication library that I’ve used many times in recent years. To connect to the server from a flash client I’ve had to rely on a library I found on github called FlashSocket.IO, actually on improved fork of an earlie, now stagnant project.

Socket.IO has recently made it to a 1.0 release, which is long overdue seeing as many people have been using it in production for ages. However this is not a ‘finally stable’ 1.0 release. This is more like a 2.0 release with a complete rewrite of the underlying engine and many API and protocol changes. If you were only using it via the official js client and not trying to inspect the internals at all then you can probably just update without a hitch.

However for my flash client this meant trouble. So, I’ve gone through and updated the flash client to work with 1.0, and added a few other features too, such as ssl compatibility (essential for working reliably with public networks), querystring parameter support (for passing arbitrary parameters from the client to the server to be available at connection time) and making the FlashSocket EventDispatcher robotlegs friendly.

It’s up here, so if you need 1.0 compatibility from flash have a go, and let me know if you have any problems.

20 thoughts on “Connecting to Socket.IO 1.0 from flash”

  1. I was having issues using your code against Socket.IO version 1.0.6. Please accept my pull request on your repo titled “v1.0.x-cleod9-event-fix”. This takes care of a couple bugs, one of which is that socket.on(, … ) does nothing when using Objects instead of Arrays within packet data.

    For anyone else stumbling upon this thread, the changes I made are as follows:

    1) Open FlashSocket.as

    2) Remove the following import statement: (package is not in the repo, nor is it needed)

    import uk.co.wilding.utilities.recursiveTrace;

    3) Change the following line:

    this.channel = URLUtil.pathname || “”;

    to

    this.channel = URLUtil.pathname || “/”;

    (Latest version of Socket.IO apparently now uses a single “/” as the default channel)

    4) Update the send() function as follows: After the if (msg as Array) {} block, please write the following:

    else
    {
    msg = [event, msg]; //This seems to be the format needed when msg is an Object
    }

    5) Lastly as an added bonus, I added a public close() function that cleans events and closes the connection 😉

    Thanks!

    –Greg

    P.S. This also fixes a bug where callback functions would cause the Socket.IO server to crash when using encoding an Object instead of an Array. In the old version of Socket.IO (0.9.*) I would pass an Object when using send() and it worked fine. But in the new version it would cause the server to crash from trying to perform non-existant array functions such as push() on the object.

    1. Hi Greg, this looks great, thanks. I was using custom namespaces and array payloads only so didn’t spot those. I’ll give it a check against my app later and merge.

      1. No problem! And btw, this thread was a life saver, I thought I’d never be able to upgrade to Socket.IO 1.0.6 so I really appreciate you sharing your source code!

  2. Hi Robert,

    It’s great you developed a solution to the problem with Socket.IO 1.x and even greater that you shared it.

    I was just wondering if you clone was a drop in replacement of the old FlashSocket.IO? I just did that (I mean I replaced the old swc with yours) and I am still getting:
    { “code”: 0, “message”: “Transport unknown” }

    Are there any tweaks that need to be implemented or I am just not looking in the right direction?

    I just updated to Socket.IO 1.1.0, BTW.

    Thanks in advance.

    Cheers,

    Atanas.

  3. This is awesome! I was struggling the past couple days with FlashSocketIO to work with latest socketIO. Question…is there a demo? Confused about which class would be the entry point. Also, any idea if this might work with very latest SocketIO?

  4. Hi there,

    I was able to connect to a local server with socket.io ver 0.9.

    However with the latest version of socket.io I get the following error: unkown transport “undefined”.

    What should I do?

    Thanks in advance.

    1. Hi Joe, I’ve not needed to return to this for a while – my 1.0 fork was built to operate with server version 1.0 – I’ve not tried it with subsequent releases. Try checking out the 1,.0 release rather than the very latest.

        1. Not without getting my hands dirty, which I don’t have much time for right now 🙁

          You might need to configure your server to accept the appropriate transport, seeing as that’s what the error is about?

    1. Hi Marcela, it’s not something I’ve noticed – however as I said above, I’ve not had reason to use this library myself for a while. I’m surprised that I’ve not seen it though – I’d be pulling my hair out if this had happened to me… Maybe log it as an issue on the github repo, quoting the SO link and when there’s a fix we can merge it in.

      Thanks

      1. Thanks for your response. I’ve only used github a handful of times, and I can’t seem to find where I can log an issue. I’m not seeing the “issues” button where it normally is (between the “code” and “pull requests” buttons).

        1. Hi Marcela, Sorry, I added issues to the repo and forgot to report back. Did you get anywhere with this?

          1. Robin,
            Thank you for opening up the issues section on the repo.

            I have not had any luck with the issue. On a whim, I tried swapping out the WebSocket stuff (from gimite to worlize) and I’m seeing the same exact issue.

            Can you tell me what server you’ve tested this with?

            (sorry for the duplicate comment, I didn’t realize I initially responded on the main thread)

  5. Robin,
    Thank you for opening up the issues section on the repo.

    I have not had any luck with the issue. On a whim, I tried swapping out the WebSocket stuff (from gimite to worlize) and I’m seeing the same exact issue.

    Can you tell me what server you’ve tested this with?

    1. I was using the main node socket.io server 1.0.6 when I updated the flash code. I might get a bit of time in a week or two to get a look at it, I’ll let you know

  6. Hi! I have problems trying to use FlashSocket.IO. First of all, Adobe Flash Builder doesn’t find “com.pnwrain.flashsocket.FlashSocket” and “com.pnwrain.flashsocket.events.FlashSocketEvent” libraries, I imported the “WebSocket.swc” library to the Referenced Libraries but I don’t know how to import the other libraries. There’s anything else that I’m doing wrong? Can you help me? A bit of code maybe could help me better… Thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *