socket.io - Python running Socket IO with another task -
i running socketio server, it's ok. write message , reply message must control page long-polling.
i can't implement socketio server.
my socketio server code:
from socketio import socketio_manage socketio.server import socketioserver socketio.namespace import basenamespace socketio.mixins import roomsmixin, broadcastmixin class chatnamespace(basenamespace, roomsmixin, broadcastmixin): def on_nickname(self, nickname): self.request['nicknames'].append(nickname) self.socket.session['nickname'] = nickname self.broadcast_event('announcement', '%s has connected' % nickname) self.broadcast_event('nicknames', self.request['nicknames']) # have them join default-named room self.join('main_room') def recv_disconnect(self): # remove nickname list. nickname = self.socket.session['nickname'] self.request['nicknames'].remove(nickname) self.broadcast_event('announcement', '%s has disconnected' % nickname) self.broadcast_event('nicknames', self.request['nicknames']) self.disconnect(silent=true) def on_user_message(self, msg): self.emit_to_room('main_room', 'msg_to_room', self.socket.session['nickname'], msg) print "%s : %s"%(self.socket.session['nickname'], msg) def recv_message(self, message): print "ping!!!", message class application(object): def __init__(self): self.buffer = [] self.request = { 'nicknames': [], } def __call__(self, environ, start_response): path = environ['path_info'].strip('/') if path.startswith("socket.io"): socketio_manage(environ, {'': chatnamespace}, self.request) else: return not_found(start_response) def not_found(start_response): start_response('404 not found', []) return ['<h1>not found</h1>'] if __name__ == '__main__': print 'listening on port 8080 , on port 843 (flash policy server)' socketioserver(('0.0.0.0', 8080), application(), resource="socket.io", policy_server=true, policy_listener=('0.0.0.0', 10843)).serve_forever()
site control code : class site_control(object):
def __init__(self): self.s_url = "http://control.site.com/getupdates" try: self.last_update_id = open('last_update_id.txt', 'r').read() except exception, e: self.last_update_id = 0 print self.last_update_id self.url = "http://control.site.com/getupdates?offset=%s"%self.last_update_id self.getupdates() def getupdates(self): response = requests.get(self.url) data_json = json.loads(response.content) data_result = data_json['result'] if int(data_result[-1]["update_id"]) != int(self.last_update_id): readresponse(data_result) self.last_update_id = data_result[-1]["update_id"] last_update_id_file = open('last_update_id.txt', 'wb') last_update_id_file.write(str(self.last_update_id)) last_update_id_file.close() self.url = "%s?offset=%s"%(self.s_url, self.last_update_id) else: print data_result[-1]['update_id'] time.sleep(1) self.getupdates() def readresponse(self, data): self.sendmsg('control ok!') #now must send message user via socket io server...
i'm sorry english bad. drew chart maybe helps.
Comments
Post a Comment