使用python在Google App Engine上实现用于Android推送消息传递的CCS

时间:2013-08-18 14:06:42

标签: android google-app-engine google-cloud-messaging

我正在尝试在Google App Engine上实现我的Android应用程序的CCS服务器端。托管在我的本地计算机上时代码工作正常,我可以将Push消息发送到我的设备。但是,当我在GAE上部署相同内容时,它会抛出以下错误

2013-08-18 06:54:55.950 / 500 11ms 0kb Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
106.197.45.136 - - [18/Aug/2013:06:54:55 -0700] "GET / HTTP/1.1" 500 0 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" "mad-push.appspot.com" ms=12 cpu_ms=21 app_engine_release=1.8.3 instance=00c61b117c247a73c2df78d7e42f9a5653723e54
E 2013-08-18 06:54:55.941
Invalid debugflag given: socket
E 2013-08-18 06:54:55.942
DEBUG: 
E 2013-08-18 06:54:55.943
DEBUG: Debug created for /base/data/home/apps/s~mad-push/1.369591069415732344/xmpp/client.py
E 2013-08-18 06:54:55.943
DEBUG:  flags defined: socket
E 2013-08-18 06:54:55.944
DEBUG: socket       start Plugging <xmpp.transports.TCPsocket instance at 0x10928c88> into <xmpp.client.Client instance at 0x10928c10>
E 2013-08-18 06:54:55.946
DEBUG: socket       stop  Plugging <xmpp.transports.TCPsocket instance at 0x10928c88> out of <xmpp.client.Client instance at 0x10928c10>.
E 2013-08-18 06:54:55.946
Client instance has no attribute 'Dispatcher'
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~mad-push/1.369591069415732344/main.py", line 70, in get
    auth = client.auth(USERNAME, PASSWORD)
  File "/base/data/home/apps/s~mad-push/1.369591069415732344/xmpp/client.py", line 214, in auth
    while not self.Dispatcher.Stream._document_attrs and self.Process(1): pass
AttributeError: Client instance has no attribute 'Dispatcher'
E 2013-08-18 06:54:55.948
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 223, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~mad-push/1.369591069415732344/main.py", line 70, in get
    auth = client.auth(USERNAME, PASSWORD)
  File "/base/data/home/apps/s~mad-push/1.369591069415732344/xmpp/client.py", line 214, in auth
    while not self.Dispatcher.Stream._document_attrs and self.Process(1): pass
AttributeError: Client instance has no attribute 'Dispatcher'

请注意我在GAE上的应用程序还包含本地的xmpppy库文件夹。我从xmpppy.sourceforge.net/

下载了源代码

这是我在GAE中部署的测试代码

import webapp2
import sys, json, random, string, xmpp
SERVER = 'gcm.googleapis.com'
PORT = 5235
USERNAME = 'My App Id'
PASSWORD = 'API Key'
REGISTRATION_ID = 'Device registration Id'

unacked_messages_quota = 1000
send_queue = []

# Return a random alphanumerical id
def random_id():
  rid = ''
  for x in range(8): rid += random.choice(string.ascii_letters + string.digits)
  return rid

def message_callback(session, message):
  global unacked_messages_quota
  gcm = message.getTags('gcm')
  if gcm:
    gcm_json = gcm[0].getData()
    msg = json.loads(gcm_json)
    if not msg.has_key('message_type'):
      # Acknowledge the incoming message immediately.
      send({'to': msg['from'],
            'message_type': 'ack',
            'message_id': msg['message_id']})
      # Queue a response back to the server.
      if msg.has_key('from'):
        # Send a dummy echo response back to the app that sent the upstream message.
        send_queue.append({'to': msg['from'],
                           'message_id': random_id(),
                           'data': {'pong': 1}})
    elif msg['message_type'] == 'ack' or msg['message_type'] == 'nack':
      unacked_messages_quota += 1

def send(json_dict):
  template = ("<message><gcm xmlns='google:mobile:data'>{1}</gcm></message>")
  client.send(xmpp.protocol.Message(node=template.format(client.Bind.bound[0], json.dumps(json_dict))))

def flush_queued_messages():
  global unacked_messages_quota
  while len(send_queue) and unacked_messages_quota > 0:
    send(send_queue.pop(0))
    unacked_messages_quota -= 1



class MainHandler(webapp2.RequestHandler):
    def get(self):
        client = xmpp.Client('gcm.googleapis.com', debug=['socket'])
        client.connect(server=(SERVER,PORT), secure=1, use_srv=False)
        auth = client.auth(USERNAME, PASSWORD)
        if not auth:
            self.response.out.write('Failed')
            sys.exit(1)
        client.RegisterHandler('message', message_callback)
        send_queue.append({'to': REGISTRATION_ID,'message_id': 'reg_id','data': {'message_destination': 'RegId','message_id': random_id()}})
        while True:
            client.Process(1)
            flush_queued_messages()



app = webapp2.WSGIApplication([('/', MainHandler)],debug=True)

1 个答案:

答案 0 :(得分:0)

根据https://developers.google.com/cloud-messaging/server#role

  

请注意,Google AppEngine不支持与XMPP(CCS)的连接。