-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmychannel.py
More file actions
102 lines (82 loc) · 3.14 KB
/
Copy pathmychannel.py
File metadata and controls
102 lines (82 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python2.4
#
# Copyright 2010 Google Inc. All Rights Reserved.
# pylint: disable-msg=C6310
"""Channel Tic Tac Toe
This module demonstrates the App Engine Channel API by implementing a
simple tic-tac-toe game.
"""
import datetime
import logging
import os
import random
import re
from django.utils import simplejson
from google.appengine.api import channel
from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import hashlib
receiverid = "receiverappid"
senderid = "senderappid"
def randomString(length):
s = hashlib.sha256()
ret = "a"
while len(ret) < length:
s.update(str(random.random()))
ret += s.hexdigest()
return ret[0:length]
class MainPage(webapp.RequestHandler):
"""The main UI page, renders the 'index.html' template."""
def get(self):
"""Renders the main page. When this page is shown, we create a new
channel to push asynchronous updates to the client."""
path = os.path.join(os.path.dirname(__file__), 'index.html')
template_values = {'senderid': senderid, 'receiverid': receiverid}
self.response.out.write(template.render(path, template_values))
class CreateChannel(webapp.RequestHandler):
def get(self):
randomness = randomString(5)
token = channel.create_channel(randomness)
self.response.out.write('Appid created: ' + randomness)
self.response.out.write('<div></div>')
self.response.out.write('Token created: ' + token)
self.response.out.write('<div></div>')
self.response.out.write('<div style="float:left;"><a href="/">Home</a></div>')
class SendMessage(webapp.RequestHandler):
def get(self):
token = channel.create_channel("senderappid")
template_values = {'token': token,
'appid': 'receiverappid',
'message': "helloworld"}
path = os.path.join(os.path.dirname(__file__), 'sendMessage.html')
self.response.out.write(template.render(path, template_values))
#self.response.out.write('AppID used to send: ' + randomness)
#self.response.out.write('Token used to send: ' + token)
class ReceiveMessage(webapp.RequestHandler):
def get(self):
token = channel.create_channel("receiverappid")
template_values = {'token': token,
'appid': 'receiverappid'}
path = os.path.join(os.path.dirname(__file__), 'receiveMessage.html')
self.response.out.write(template.render(path, template_values))
#self.response.out.write('AppID used to send: ' + randomness)
#self.response.out.write('Token used to send: ' + token)
class PostMessage(webapp.RequestHandler):
def post(self):
message = "helloworld"
receiverid = "receiverappid"
channel.send_message(receiverid, message)
self.response.out.write("message sent")
application = webapp.WSGIApplication([
('/', MainPage),
('/createchannel', CreateChannel),
('/sendmessage', SendMessage),
('/postmessage', PostMessage),
('/receivemessage', ReceiveMessage)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()