If you can't read please download the document
Upload
ian-lewis
View
2.403
Download
3
Embed Size (px)
DESCRIPTION
Twisted でチャットサーバの作り方
Citation preview
2.
8. 9. 10.
16. 17. 18.
23. 24. Twisted Chat Server
29. Twisted 30. Long Polling $.ajax({ type: "GET", url: "http://localhost:8080/", async: true, /* If set to non-async, browser shows page as "Loading.."*/ cache: false, timeout:50000, /* Timeout in ms */ dataType: "jsonp", jsonp: "callback", success: function(data){ add_msg(data); setTimeout( 'waitForMsg()', /* Request next message */ 1000 /* ..after 1 seconds */ ); }, 31. Long Polling class ChatServer(resource.Resource): def render_GET(self, request): chat_d = defer.Deferred() chat_d.addCallback(partial(chat_msg, request)) chat_d.addErrback(println, "chat error") self.deferred_list.append(chat_d) return server.NOT_DONE_YET 32. Message Post class ChatServer(resource.Resource): deferred_list = [] def render_POST(self, request): newdata = request.content.getvalue() data = json.loads(newdata) for i in range(len(self.deferred_list)): d = self.deferred_list.pop() d.callback(data) return 'OK' 33. Long Polling def chat_msg(request, chat_msg): callback = request.args.get("callback")[0]; request.setHeader('Content-Type', 'application/javascript') request.write("%s(%s)" % (callback, json.dumps(chat_msg))) request.finish() 34. [email_address] @IanMLewis