1. SwfConduit

    A Flash Socket Server

    Doug Bell

    Double Cluepon Software Corp.

  2. Flash Remoting

  3. The Major Players: BlazeDS

  4. The Major Players: Red5

  5. The Major Players: Smartfox Server

  6. A Challenger Appears!

  7. Get SwfConduit

  8. Write a Server

  9. Create the Message

    # Chat message class
    from swfconduit.event import Event
    class ChatEvent( Event ):
        # The nickname of the user sending the message
        nickname = ""
        # The message
        text = ""
    
  10. fire()

        def fire( self, server, session ):
            # We recieved a chat event, send it to every
            # other person
            for session_id in server.sessions:
                s = server.sessions[session_id]
                if ( s is not session ):
                    s.sendEvent( self )
    
  11. Register the Message

    # Register our ChatEvent class. the first argument is the same as the
    # registerClassAlias string in the client
    import pyamf
    pyamf.register_class( ChatEvent, "swfconduit.chat.ChatEvent" )
    
  12. Configure the Server

    # Create the SwfConduit server
    from swfconduit.server import Server
    server = Server({ "proto" : "tcp", "port" : 8000 })
    
    # Load and run SwfConduit
    from swfconduit.loader import Loader
    loader = Loader()
    loader.servers.append( server );
    application = loader.get_application()
    
  13. Run chat.py

    twistd -ny chat.py
    C:\Users\Doug\swfconduit\examples\chat>twistd.py -ny chat.py
    2012-05-21 16:11:57-0500 [-] Log opened.
    2012-05-21 16:11:57-0500 [-] twistd 12.0.0 (C:\Python27\python.exe 2.7.3) starting up.
    2012-05-21 16:11:57-0500 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
    2012-05-21 16:11:57-0500 [-] SocketPolicyFactory starting on 843
    2012-05-21 16:11:57-0500 [-] Starting factory <swfconduit.socketpolicy.SocketPolicyFactory instance at 0x0279A760>
    2012-05-21 16:11:57-0500 [-] Server starting on 8000
    2012-05-21 16:11:57-0500 [-] Starting factory <swfconduit.server.Server instance at 0x026D1BC0>
    
  14. Create the Client

  15. ChatEvent class

    package swfconduit.chat {
        import swfconduit.Event;
        public class ChatEvent extends swfconduit.Event {
            /** The person who sent the event */
            public var nickname:String;
    
            /** The message being sent/received */
            public var text:String;
    
            /** Construct a ChatEvent. Default values are required. */
            public function ChatEvent( nick:String="", text:String="" ) {
                this.nickname = nick;
                this.text = text;
            }
        }
    }
    
  16. Chat Application

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application width="400" height="300" 
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx" 
    >
        <s:layout><s:VerticalLayout /></s:layout>
        <s:RichEditableText editable="false" id="chatbox" 
                width="100%" height="250">
            <s:text>*** Type /nick NEW_NAME to change your 
                nickname</s:text>
        </s:RichEditableText>
        <s:TextInput id="input" width="100%" height="25" />
        <s:Label text="You are {nickname}" height="20" />
    </s:Application>
    
  17. Chat Application Script

    <fx:Script><![CDATA[
        import swfconduit.Socket;
        import swfconduit.chat.ChatEvent;
    
        /** The server to connect to */
        public var hostname:String = "localhost";
    
        /** The port to connect to */
        public var port:uint = 8000;
    
        /** Our socket object */
        public var socket:swfconduit.Socket;
    
        /** The user's current nickname */
        [Bindable]
        public var nickname:String;
    
  18. Connect to the Server

    public function init():void {
        // Register our event class
        registerClassAlias( "swfconduit.chat.ChatEvent", ChatEvent );
    
        // Connect to the server
        socket = new swfconduit.Socket( hostname, port );
    }
    
  19. Handle incoming chat

    public function handleChat(event:ChatEvent):void {
        chatbox.appendText( "\n" + event.nickname + ": " + event.text );
    }
    
  20. Send outgoing chat

    public function sendChat():void {
        // Let the user change their nickname using /nick NewName
        if ( input.text.match('^/nick') ) {
            // Get everything after "/nick "
            nickname = input.text.substr(6); 
        }
        else {
            var message:ChatEvent 
                = new ChatEvent( nickname, input.text );
            socket.writeEvent( message );
            chatbox.appendText( "\n" + nickname + "> " + input.text );
        }
        input.text = "";
    }
    
  21. Run it!

        # Compile chat.mxml into chat.swf
        mxmlc -l+=swfconduit/flex chat.mxml
    
  22. The End!

    Slides are licensed under a CC-BY-SA 3.0 license.

    Code is licensed under the GNU GPL v3.0 or later (the same terms as SwfConduit itself).