Created
September 13, 2012 22:45
-
-
Save scragg0x/3718282 to your computer and use it in GitHub Desktop.
Tornadio2 FFXI Shout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import brukva | |
import json | |
import phpserialize | |
from tornado import web | |
from tornadio2 import SocketConnection, TornadioRouter, event, SocketServer | |
c = brukva.Client() | |
c.connect() | |
pubsub = brukva.Client() | |
pubsub.connect() | |
class ShoutsConnection(SocketConnection): | |
server_id = None | |
clients = set() | |
def __init__(self, *args): | |
pubsub.subscribe('ffxi.shouts') | |
pubsub.listen(self.shout) | |
super(ShoutsConnection, self ).__init__(*args) | |
@event | |
def join(self, server_id): | |
print "Joining " + server_id | |
self.server_id = server_id | |
self.clients.add(self) | |
c.hget('ffxi.shouts', self.server_id, self.init_shouts) | |
def init_shouts(self, data): | |
self.emit('shouts', json.loads(data)) | |
def shout(self, m): | |
shouts = json.loads(m.body) | |
for client in self.clients: | |
client.emit('shouts', shouts.get(client.server_id)) | |
#self.emit('shouts', json.loads(m.body)) | |
def on_close(self): | |
print "Closing" | |
if self in self.clients: | |
self.clients.remove(self) | |
ShoutsRouter = TornadioRouter(ShoutsConnection, dict(enabled_protocols=['websocket', 'xhr-polling'])) | |
application = web.Application(ShoutsRouter.urls, socket_io_port=443, socket_io_address='216.144.246.171') | |
if __name__ == "__main__": | |
#import logging | |
#logging.getLogger().setLevel(logging.DEBUG) | |
SocketServer(application) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment