Flash Socket Server in Python

In this example I am going to show a socket server that I use to catch my output messages when developing Flash games with MTASC. This server maintains a connection with the Flash swf and messages are passed from Flash via an XMLSocket instance. I've found this to be the most efficient way to get debug messages from your movie when it's on a remote machine or compiled with MTASC.

This server makes use of the excellant Twisted web framework for Python. I highly recommend Twisted if you are interested in programming servers with Python, because once you learn it, it is very easy to crank out any type of server in minutes.

PYTHON
  1. from twisted.protocols import basic
  2. from twisted.internet import reactor, protocol
  3.  
  4. class MyReceiver(basic.LineReceiver):
  5.  
  6.   delimiter = '\0'
  7.  
  8.   def connectionMade(self):
  9.     print "Got new client!"
  10.     self.factory.clients.append(self)
  11.        
  12.   def connectionLost(self, reason):
  13.     print "Lost a client!"
  14.     self.factory.clients.remove(self)
  15.  
  16.   def lineReceived(self,line):
  17.  
  18.     if line == "<policy-file-request/>":
  19.       print "\t sending policy file"
  20.       self.sendLine("<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"8007\" /></cross-domain-policy>")
  21.     else:
  22.       print line
  23.                
  24. class XMLSocket(protocol.Factory):
  25.  
  26.   clients=[]
  27.  
  28.   def __init__(self, protocol=None):
  29.     self.protocol=protocol
  30.  
  31. def main():
  32.  
  33.   reactor.listenTCP(8007, XMLSocket(MyReceiver))
  34.   reactor.run()
  35.  
  36. if __name__ == '__main__':
  37.   main()

Actionscript
  1. function someInitFunction(){
  2.   System.security.loadPolicyFile("xmlsocket://192.168.0.1:8007");
  3.  
  4.   _global.socket = new XMLSocket();
  5.   _global.socket.connect("192.168.0.1", "8007");
  6. }
  7.  
  8. static function myTrace(msg){
  9.  
  10.   // I like to report the time with my output.
  11.   var t = getTimer()/1000;
  12.  
  13.   _global.socket.send("[" + t + "] " + msg);
  14. }

If you run the server, it will listen on port 8007. You can, of course, change that port if you like, but be sure to change it in the Flash, and also in the cross-domain-policy string. The Flash movie is also expecting the server to be listening on the IP address 192.168.0.1. If you are running the server and the swf on the same machine, then you can change that to localhost. I like to have it point to the address of my machine, because then I can freely move the swf to a staging machine, and the debug messages will still get there.

I believe this server could be modified to support multiplayer games. It keeps track of all the connections, so you could start forwarding messages around. I haven't tried this yet, but I hope to in the near future.

6 Responses to “Flash Socket Server in Python”

  1. bilbon Says:

    Thx for the tip.

    I’m a python developper and i’m starting to code a game in flash. TwistedMatrix seems great !

  2. Dave Cook Says:

    Hi:
    Did you ever get around to adjusting your program for multiplayer games?
    Best regards,
    Dave

  3. austin Says:

    Hi Dave,

    I haven’t worked on any multiplayer stuff, yet. I’ve actually moved to Lisp, instead of Python, for the backend and plan to try out some multiplayer stuff there.

    -austin

  4. bilbon Says:

    Hello

    I think dave’s question is for me ?

    I’m at the begining of the project. After testing it, Twisted is really good and i’m gonna keep it for this project and other surely. Actually i’m studing how to organize flash developpement to separate actionscript and animations. Multiplayer adjustement are not planned but all advertising and help is welcome ;)

  5. Gavroche Le Gnou Says:

    Another XML socket server for flash written in python : Palabre
    With Room support, nickname, …
    http://palabre.gavroche.net

  6. wibrst Says:

    Good job!dynamic language collaborate!