Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding problem in chat example #56

Open
jorgecarleitao opened this issue Nov 4, 2013 · 2 comments
Open

Encoding problem in chat example #56

jorgecarleitao opened this issue Nov 4, 2013 · 2 comments

Comments

@jorgecarleitao
Copy link

I'm using the chat example, and I'm getting some problems in the encoding. Specifically, if I use the letter "á" in a message inside a room, the connection drops.

To reproduce the error, use the example along with

sudo python manage.py runserver_socketio 127.0.0.1:9000

I just start using it, so I don't have so much experience to tackle the error. Nevertheless, I hope the attached log from the socket_io helps.

127.0.0.1 - - [2013-11-04 15:36:33] "Socket.IO message: {u'action': u'start', u'room': 3, u'name': u'test_subject'}"
Traceback (most recent call last):
  File "/Users/user1/env/lib/python2.7/site-packages/gevent/greenlet.py", line 390, in run
    result = self._run(*self.args, **self.kwargs)
  File "/Users/user1/env/lib/python2.7/site-packages/socketio/server.py", line 49, in handle
    handler.handle()
  File "/Users/user1/env/lib/python2.7/site-packages/gevent/pywsgi.py", line 180, in handle
    result = self.handle_one_request()
  File "/Users/user1/env/lib/python2.7/site-packages/gevent/pywsgi.py", line 314, in handle_one_request
    self.handle_one_response()
  File "/Users/user1/env/lib/python2.7/site-packages/socketio/handler.py", line 68, in handle_one_response
    jobs = self.transport.connect(session, request_method)
  File "/Users/user1/env/lib/python2.7/site-packages/socketio/transports.py", line 101, in connect
    return self.handle_post_response(session)
  File "/Users/user1/env/lib/python2.7/site-packages/socketio/transports.py", line 67, in handle_post_response
    messages = self.decode(data)
  File "/Users/user1/env/lib/python2.7/site-packages/socketio/transports.py", line 16, in decode
    return self.handler.environ['socketio'].decode(data)
  File "/Users/user1/env/lib/python2.7/site-packages/socketio/protocol.py", line 113, in decode
    messages.append(json.loads(data[3:size]))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 83 (char 83)
<Greenlet at 0x1027cd5f0: <bound method SocketIOServer.handle of <SocketIOServer at 0x10270e2d0 fileno=3 address=127.0.0.1:9000>>(<socket at 0x10275f450 fileno=[Errno 9] Bad file d, ('127.0.0.1', 57567))> failed with ValueError
@stephenmcd
Copy link
Owner

Good news is I can reproduce this.

Bad news is that at a glance it appears to be an error in one of the dependencies, either gevent-websocket or gevent-socketio. Now currently django-socketio is tied to very outdated versions of those, so in all likelihood, the bug may have already been fixed in those.

So this issue is more or less dependant on #19 being resolved, at which point this may no longer even be a problem. I'll leave it open for now just in case.

@yguarata
Copy link

In case anyone is still facing this problem, my workaround on it consisted in converting the socket message to an array of char codes and stringified with JSON. When receiving the message, it is need to parse the message back to the array of code (JSON.parse), and converting it to string again. See helper functions bellow:

function stringify_char_coded(json_obj) {
    var stringfied = JSON.stringify(json_obj);
    var char_codes = [];
    for (var i=0; i < stringfied.length; i++) {
        char_codes.push(stringfied.charCodeAt(i));
    }
    return JSON.stringify(char_codes);
}

function parse_char_coded(str) {
    var char_codes = JSON.parse(str);
    var s = "";
    for (var i=0; i < char_codes.length; i++) {
        s += String.fromCharCode(char_codes[i]);
    }
    return JSON.parse(s);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants