通过谷歌应用引擎渠道api发送数据uri

时间:2013-07-30 21:48:11

标签: javascript python html5 google-app-engine channel-api

我正在尝试通过通道api将从canvas元素中获取的图像的数据uri发送到另一个客户端。

这是我的javascript:

var pictData = imageCanvas.toDataURL("image/png");
sendPictData(pictData);

function sendPictData(pictData){
    dataToSend = encodeURIComponent(pictData);
    sendMessage({type: 'blackboardBackground',
                image: dataToSend
              });
}

function sendMessage(message){
    var msgString = JSON.stringify(message);
    path = '/lessonarea/message?r={{ key }}' + '&u={{ me }}';
    var xhr = new XMLHttpRequest();
    xhr.open('POST', path, true);
    xhr.send(msgString);

}

我不断从应用引擎日志中收到错误“InvalidMessageError”

sendMessage()函数在发送纯文本时工作正常但我似乎无法发送数据uri。

如上所示,我试图在方法sendPictData()中对url进行编码,但这似乎没什么帮助。

感谢您提供的任何帮助。

更新 - Python处理程序代码和日志输出:

class MessagePage(webapp2.RequestHandler):
    def post(self):
        message = self.request.body
        lessonRoomKey = self.request.get('r')
        user = self.request.get('u')
        with LOCK:
            lesson_room = LessonRoom.get_by_id(lessonRoomKey)
            if lesson_room:
                self.handle_message(lesson_room, user, message)

        else:
            logging.warning('Unknown Lesson room ' + lessonRoomKey)

    def handle_message(self, lessonRoom, user, message):
        message_obj = json.loads(message)
        logging.info("Message type = " + message_obj['type'])
        other_user = lessonRoom.get_other_user(user)
        lessonRoomKey = lessonRoom.key.id();
        if other_user and lessonRoom.has_user(other_user):
            on_message(lessonRoom, other_user, message)

    def on_message(self, room, user, message):
        client_id = make_client_id(room, user)
        if room.is_connected(user):
            channel.send_message(client_id, message)

这是日志的输出:

INFO     2013-07-30 21:39:22,582 lessonarea.py:364] Message type = blackboardBackground
ERROR    2013-07-30 21:39:22,583 webapp2.py:1553] 
Traceback (most recent call last):
File "/home/alec/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
  rv = self.handle_exception(request, response, e)
File "/home/alec/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
  rv = self.router.dispatch(request, response)
File "/home/alec/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
  return route.handler_adapter(request, response)
File "/home/alec/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
  return handler.dispatch()
File "/home/alec/google_projects/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
  return self.handle_exception(e, self.app.debug)
File "/home/alec/google_projects/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
  return method(*args, **kwargs)
File "/home/alec/google_projects/src/lessonarea.py", line 357, in post
  self.handle_message(lesson_room, user, message)
File "/home/alec/google_projects/src/lessonarea.py", line 385, in handle_message
  on_message(lessonRoom, other_user, message)
File "/home/alec/google_projects/src/lessonarea.py", line 168, in on_message
  channel.send_message(client_id, message)
File "/home/alec/google_projects/google_appengine/google/appengine/api/channel/channel.py", line 209, in send_message
  raise InvalidMessageError
InvalidMessageError

如前所述,我认为处理程序正常工作,因为它在发送纯文本消息时工作正常。

1 个答案:

答案 0 :(得分:1)

在dragonx的评论中发现了答案 - 我的简单疏忽。

频道消息超过了32 kb的限制。改变了图像的大小,代码工作正常。