# Chat message class from swfconduit.event import Event class ChatEvent( Event ): # The nickname of the user sending the message nickname = "" # The message text = ""
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 )
# 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" )
# 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()
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>
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; } } }
<?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>
<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;
public function init():void { // Register our event class registerClassAlias( "swfconduit.chat.ChatEvent", ChatEvent ); // Connect to the server socket = new swfconduit.Socket( hostname, port ); }
registerClassAlias()
given the same string the server hasapplicationComplete="init()"
to <s:Application>
public function handleChat(event:ChatEvent):void { chatbox.appendText( "\n" + event.nickname + ": " + event.text ); }
socket.addEventListener( "ChatEvent", handleChat );
to init()
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 = ""; }
enter="sendChat()"
to s:TextInput
# Compile chat.mxml into chat.swf mxmlc -l+=swfconduit/flex chat.mxml
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).