sobota, 2 lipca 2011

Simple http push server for flash using python twisted

I was preety busy at work. Sorry for not posting. I will try to post once a month from now.The networing profiler project was stopped due to my work but I will try to finish it's simple and post the sources. To the point.

In my workplace we are pushing some data to the clients, and more websites are pushing realtime data so it made me think. How hard it's to write simple http push server for actionscript using python ?

Well it seems not so hard cause we got great library for it twisted. So let's get started on server side:

So we start by creating simple class Test with GET and POST handlers and in get we add server.NOT_DONE_YET to never stop sending GET request once connected. In __init__ method we declare array for users and array for messages to pass. Next we add LoopingCall task to throtle messages for our clients every 250ms, and define method __print_time that we will be using to create messages. Well __print_time is simple method that we pass time and messages when there are any in our messages array.We simply loop in our users array and add aditional message to send.

Next what about client, some time ago I started simple library for my inhouse projects.I created simple HTTPConnection class where I use URLStream for handling connection and some self created Observer for listening events instead of eventDispatcher.Well I will not describe my classes here but only show the main class for the server class in python.The main method is init where we create gui, HTTPConnection named consumer and some logger. We add event for progress where we simply try to parse json using as3corelib and if we have some messages we try to display it in our gui. when we press ENTER in our upper TextField we are simply sending post to the same url.

Things to improve:1. detect when disconnected and try to reconnect2. parse messages only when we need

And that's it. The exported swf size is wow 12KB, not much :)Any questions please post in comments.Sources are below, i will try to post my HTTPConnection class sources with others on github as soon as I will have some more time.Just run twisted_stream.py in server folder and Main.swf in client/bin-release