Binary Socket

Introduction

This page describes how to setup the Socket example application for PyAMF.

The Socket class in Actionscript 3.0 enables Adobe Flash Player applications to make socket connections and to read and write raw binary data.

The example server shows how to setup a Socket connection with the Flex SDK and exchange AMF data with a Twisted server.

A live demo can be found on the PyAMF blog.

Download

Grab the example from SVN with:

svn export http://svn.pyamf.org/trunk/doc/tutorials/examples/actionscript/socket socket-example
cd socket-example/python

Alternatively, if you just want to take a look at the source, you can browse the example online.

Gateway

Note: Make sure you have Twisted >= 2.5 installed.

The Twisted socket server for the Adobe Flash Player and Python AMF clients starts on http://localhost:8000 when you run:

twistd -noy timer.tac

You should see something like:

2008-01-07 06:03:09+0100 [-] Log opened.
2008-01-07 06:03:09+0100 [-] twistd 2.5.0 (../Python 2.5.1) starting up
2008-01-07 06:03:09+0100 [-] reactor class: <class 'twisted.internet.selectreactor.SelectReactor'>
2008-01-07 06:03:09+0100 [-] server.TimerFactory starting on 8000
2008-01-07 06:03:09+0100 [-] Starting factory <server.TimerFactory instance at 0x1414af8>

You can stop the server by pressing Ctrl-c.

If you want to run the application as a Twisted daemon process, use this command instead:

twistd -oy timer.tac

Clients

Adobe Flash Player

You can simply open the SWF file and it will connect to http://localhost:8000.

Use the ‘Start’ and ‘Stop’ buttons to control the Python socket server that sends the system time to the Flex application every second:

../../_images/binarysocket.jpg

Python

The Python AMF client can be started by running the following from the python folder:

python client.py

The client will connect to the socket server on http://localhost:8000 and start sending messages:

Connecting to socket server on localhost:8000
Connected to server.

send request: start
2009-07-02 23:06:31.684023
2009-07-02 23:06:32.684465
2009-07-02 23:06:33.684923
2009-07-02 23:06:34.685351
2009-07-02 23:06:35.685828
2009-07-02 23:06:36.686253
send request: stop

Options

When you run python client.py –help it will display the various options available for this example client:

Usage: client.py [options]

Options:
  -h, --help            show this help message and exit
  -p PORT, --port=PORT  port number [default: 8000]
  --host=HOST           host address [default: localhost]