问题
I'm trying to send a data uri of an image that has been taken from a canvas element to another client via the channel api.
This is my 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);
}
I keep getting error "InvalidMessageError" from the app engine log
the sendMessage() function works fine when just sending plain text but i cant seem to be able to send a data uri.
As seen above I have tried to encode the url inside the method sendPictData() but this doesn't seem to help.
Appreciate any help you can give.
Update - Python handler code and log output:
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)
This is the output from the log:
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
As mentioned before I believe the handler to be working correctly as it works fine when sending a plain text message.
回答1:
Answer was discovered in the comments by dragonx - simple oversight on my part.
The channel message was exceeding the 32 kb limit. Changed the size of the images and the code worked fine.
来源:https://stackoverflow.com/questions/17958052/sending-a-data-uri-via-google-app-engine-channel-api