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.
-
from twisted.protocols import basic
-
from twisted.internet import reactor, protocol
-
-
class MyReceiver(basic.LineReceiver):
-
-
delimiter = '\0'
-
-
def connectionMade(self):
-
print "Got new client!"
-
self.factory.clients.append(self)
-
-
def connectionLost(self, reason):
-
print "Lost a client!"
-
self.factory.clients.remove(self)
-
-
def lineReceived(self,line):
-
-
if line == "<policy-file-request/>":
-
print "\t sending policy file"
-
self.sendLine("<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"8007\" /></cross-domain-policy>")
-
else:
-
print line
-
-
class XMLSocket(protocol.Factory):
-
-
clients=[]
-
-
def __init__(self, protocol=None):
-
self.protocol=protocol
-
-
def main():
-
-
reactor.listenTCP(8007, XMLSocket(MyReceiver))
-
reactor.run()
-
-
if __name__ == '__main__':
-
main()
-
function someInitFunction(){
-
System.security.loadPolicyFile("xmlsocket://192.168.0.1:8007");
-
-
_global.socket = new XMLSocket();
-
_global.socket.connect("192.168.0.1", "8007");
-
}
-
-
static function myTrace(msg){
-
-
// I like to report the time with my output.
-
var t = getTimer()/1000;
-
-
_global.socket.send("[" + t + "] " + msg);
-
}
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.
November 22nd, 2005 at 2:59 pm
Thx for the tip.
I’m a python developper and i’m starting to code a game in flash. TwistedMatrix seems great !
November 22nd, 2005 at 6:51 pm
Hi:
Did you ever get around to adjusting your program for multiplayer games?
Best regards,
Dave
November 22nd, 2005 at 7:06 pm
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
November 29th, 2005 at 1:44 pm
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 ;)
December 5th, 2005 at 2:38 pm
Another XML socket server for flash written in python : Palabre
With Room support, nickname, …
http://palabre.gavroche.net
March 30th, 2007 at 4:46 am
Good job!dynamic language collaborate!