Loading assets with Sockets

Socket Browser

During my AS3 session in July at the Flash Festival I talked about loading assets at runtime, with sockets, since we have binary sockets now, we can send any binary stream we want into the player, since it's a valid stream for the flash.display.Loader class, we can save the incoming bytes into a ByteArray then interpret the content with the Loader.loadBytes() method.

Hence, assets loaded through a socket connection will not be cached by the browser ;)

I developped a little Socket browser in C#, and a client tool in AS3, just start the Socket browser, connect the Flash client to the socket browser with the correct IP (copy paste the IP given by the Socket Browser), then select a valid file (bitmap, or swf), then the stream will be sent to the Flash client, and you should be able to see it ;)

If you want to load several files, you will have to close the Socket Browser and also the Flash client, and restart them, some little bugs to be fixed soon ;)

Download the sources here

In case you don't have .NET framework, you can download it here

Comments (31)

  1. migs wrote:

    Sweet demo.

    Do you know if it’s possible to have flash act as a socket client AND SERVER?

    Tuesday, November 21, 2006 at 6:40 am #
  2. migs wrote:

    basically… (and i think not, but i wish/hope is possible) is it possible to write a socket server in AS3? – imagine being to share files between two flash files without having a server. now, that! would be sweet.

    Tuesday, November 21, 2006 at 7:00 am #
  3. thibault.imbert wrote:

    this is not possible as far as i know ;)

    Friday, November 24, 2006 at 7:13 pm #
  4. joa ebert wrote:

    No you can’t. You’d need listening sockets which you can not create with AS3.

    Saturday, November 25, 2006 at 7:14 pm #
  5. This is the coolest thing I have seen thus far from AS3. Very powerful stuff. May I use this code freely?

    Thursday, November 30, 2006 at 1:35 am #
  6. thibault.imbert wrote:

    hi hendrix,

    yes you can use it, of course :)

    Thursday, November 30, 2006 at 11:52 am #
  7. I think I’m doing something wrong. When I run the socket server on my local pc I get ::1 on port 12000 in th ebottom window. (I installed .NET version 2)

    If I type 1 into the socket server field of the Flash client on my local system I get sandbox violations (local with file system not permitted to use sockets). I also get cross domain sandbox violations if I upload it to a webserver.

    I guess I don’t fully understand where to put the files and how to run them.

    Question 1 :
    Where do put the C# socket server?

    Question 2:
    Where do I put the Flash file?

    Question 3:
    What adjustments do I need to make to overcome the security violations?

    Question 4:
    Should the socket server be putting out something besides ::1 on port 12000 ?

    Thanks for all of your help.

    Sunday, December 3, 2006 at 4:32 pm #
  8. thibault.imbert wrote:

    hi hendrix,

    question 1 : the socket server is launched on localhost on a ip that is automatically found by the application on port 12000

    question 2 : the flash file, can be anywhere on your computer, the idea would be to run a socket server online, that would stream SWF available on your server, but for the demo, let’s send SWF on your hard disk ;)

    question 3 : you have to check first, if Window for instance is not blocking the Socket Server (firewall) so check that first, usually when you start first the application, Windows opens a window asking for blocking or not the application, another thing to check is the Security SandBox type running with your player.

    question 4 : there is nothing for me to do usually, just check that the server can run, by connecting to it through telnet for example, next check your security sandbox type, from flash.system.Security.sandboxType and make sure you have this value “localTrusted”

    cheers ;)

    let me know !

    Wednesday, December 6, 2006 at 9:10 pm #
  9. Thanks for the advice.
    I have gotten it to work an a different pc. I isolated the problem to be in how the socket server interacts with my wireless router. I am still trying to work around it. Any chance you will make the socket server source available?
    Thanks
    Clu

    Thursday, December 7, 2006 at 5:23 pm #
  10. Ali Mills wrote:

    Another way to overcome the security violations that question 3 asks about is with a Flash Player trust file. On my Win machine, it’s in C:\Documents and Settings\[USER]\Application Data\Macromedia\Flash Player\#Security\FlashPlayerTrust. The document at http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=1165eb90 talks about how to create them. Look under the heading “Trust Files”.

    Ali

    Thursday, December 7, 2006 at 10:13 pm #
  11. thibault.imbert wrote:

    interesting,

    tks Ali ;)

    Friday, December 8, 2006 at 11:34 am #
  12. spender wrote:

    Migs write: Sweet demo.

    Do you know if it’s possible to have flash act as a socket client AND SERVER?

    LocalConnection would do this. You could send the binary data over a LocaConnection to another .swf running on the same machine.

    Friday, May 18, 2007 at 1:44 pm #
  13. Thibault Imbert wrote:

    Hi spender,

    No you can’t, you would need listening sockets for that.

    Concerning the LocalConnection thing, you could send some binary data, but you can’t send more than 40kb through it. Which is quite limitating.

    Friday, May 18, 2007 at 2:17 pm #
  14. scriptMan wrote:

    Is there a way to send “byteArray” from flash to a host application (to a c# application in my example) ?

    Saturday, June 9, 2007 at 9:49 pm #
  15. Thibault Imbert wrote:

    Hi scriptMan,

    yes it is possible very easily with the different methods from the flash.net.Socket class

    have a look here :
    http://livedocs.adobe.com/flex/201/langref/flash/net/Socket.html

    cheers ;)

    Sunday, June 10, 2007 at 12:57 am #
  16. scriptMan wrote:

    Hmm, that’s quite good :)

    I’ll try this method, but, i wonder if i can make it by External Interface?

    Either way, i just want to send a byteArray to my desktop host application, and get a jpeg from it :)

    Sunday, June 10, 2007 at 12:13 pm #
  17. flatface wrote:

    scriptMan, in Flash you can encode the byteArray as a base64 using this class http://www.dynamicflash.com/goodies/base64
    then you can send it via ExternalInterface and decode it to a byteArray in your c# app

    Thursday, July 12, 2007 at 3:06 pm #
  18. Does anyone know what happened to the source code?

    Tuesday, November 27, 2007 at 3:11 am #
  19. Michael wrote:

    I would like to know if anyone has the source code.

    Monday, December 17, 2007 at 3:46 am #
  20. Thibault Imbert wrote:

    Hi guys,

    I will re-upload the sources tonight !

    kind regards
    Thibault

    Monday, December 17, 2007 at 9:45 am #
  21. miraclemaker wrote:

    Link to sources is still broken :-(

    Tuesday, January 22, 2008 at 11:55 am #
  22. Henrik wrote:

    Hi. I have a project where we need this.
    I could pay for the help.

    kindly regards
    Henrik

    Tuesday, January 29, 2008 at 11:15 pm #
  23. Lehmann wrote:

    Thanks for your work. It would be great if you could upload the sources. Best regards, Christian Lehmann

    Monday, February 4, 2008 at 12:27 am #
  24. Michael wrote:

    Source still down?
    And yes thank you very much for you work, this stuff is very interesting and I have been doing a lot of research on this stuff but can’t find much to get me on the right track.

    Sunday, March 2, 2008 at 11:56 am #
  25. Bryan wrote:

    Great article, by the way, but it appears that the link to the source code you’ve provided is not working. If you would, it would be great to see which direction you’re heading in with the socket code.

    Monday, March 31, 2008 at 11:06 pm #
  26. Ms wrote:

    No source here.. :(

    Thursday, November 13, 2008 at 4:43 pm #
  27. Thibault Imbert wrote:

    Hi Ms,

    I will re-upload the sources tonight ;)

    stay tuned,

    best,

    Thibault

    Thursday, November 13, 2008 at 5:15 pm #
  28. PS wrote:

    No source…?

    Friday, January 16, 2009 at 4:37 pm #
  29. Thibault Voisin wrote:

    Very interesting stuff, but the link to the sources seems broken…

    Thursday, July 16, 2009 at 3:26 pm #
  30. Thibault Imbert wrote:

    Hi Thibault,

    I will update that tonight, sorry about that !

    Thibault

    Thursday, July 16, 2009 at 3:42 pm #
  31. Xander wrote:

    Sadly the links are still down. I’m also interested in how you did it.

    Saturday, December 26, 2009 at 4:02 pm #