DatagramServer is small library for Flex+Parsley that allows you to set up and manage a UDP listener in your Air 2.5+ application.

UDP Packets in Flex Air apps

Datagram packets are ideal for gathering data from external devices without having to establish two-way persistent socket connections. Perfect for gathering logs, benchmark data, statistics, …etc.

Note: You can send and receive UDP packets in Air 2.5+, but only on desktop Air apps. MobileDevice, extendedMobileDevice, tv, extendedTV profiles are unsupported.

You can download the DatagramServer swc from github:

https://github.com/mere/DatagramServer

Setup

  1. In your Parsley contextbuilder register DatagramServerContext:
    <parsley:FlexConfig type="{ DatagramServerContext }"/>
    <parsley:FlexConfig type="{ DatagramServerContext }"/>
  2. If you want to edit the datagram host and port from the UI, fastinject DatagramModel, then create a two way binding to .host and .port:
    <s:TextInput text="@{data.host}" width="120" />
    <s:TextInput text="@{data.port}" width="40" />
    <s:TextInput text="@{data.host}" width="120" />
    <s:TextInput text="@{data.port}" width="40" />
  3. Start/Stop the Datagram server from any parsley-managed class, eg.::
    dispatcher(new Event(DatagramServerEvent.START));
    dispatcher(new Event(DatagramServerEvent.STOP));
    dispatcher(new Event(DatagramServerEvent.START));
    dispatcher(new Event(DatagramServerEvent.STOP));
  4. To listen to incoming UDP packages, create a parsley messagehandler:
    [MessageHandler]
    public function dataReceived(e:DataReceivedEvent):void
    {
        logs.add(e.string);
        /**
        * you can also use:
        * e.ip     : returns the IP of the sender
        * e.data     : returns the packet as a bytearray
        * e.xml     : returns the packet as xml
        */
    }
    [MessageHandler]
    public function dataReceived(e:DataReceivedEvent):void
    {
        logs.add(e.string);
        /**
        * you can also use:
        * e.ip     : returns the IP of the sender
        * e.data     : returns the packet as a bytearray
        * e.xml     : returns the packet as xml
        */
    }

How to test your Datagram Listener

You can use Netcat to send udp packages. On a mac:

nc -u your.ip.address port
nc -u your.ip.address port

Example

Check out: https://github.com/mere/DatagramServerExample

Screen shot 2011 06 30 at 10.31.04 Flex Datagram Server

Screen shot 2011 06 30 at 10.31.36 Flex Datagram Server

Tagged with:
 

One Response to Flex Datagram Server

  1. My Homepage says:

    … [Trackback]…

    [...] Read More: zoltanb.co.uk/flex-datagram-server/ [...]…

Leave a Reply

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

*


× 3 = twenty seven

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">