2

我正在尝试通过通道 api 将已从画布元素获取的图像的数据 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

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

4

1 回答 1

1

答案是在dragonx的评论中发现的——我只是简单的疏忽。

频道消息超出了 32 kb 的限制。更改了图像的大小,代码运行良好。

于 2013-08-01T08:38:23.310 回答