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

Problems with receiving 'utf-8' from client #674

Closed
bstojkovic opened this issue Mar 27, 2018 · 2 comments
Closed

Problems with receiving 'utf-8' from client #674

bstojkovic opened this issue Mar 27, 2018 · 2 comments

Comments

@bstojkovic
Copy link

I am trying to create a 2-way communication between server and client using Flask and socket.io.

Everything works fine until server receives utf-8 string from client, which gets garbled. Sending from server to client works fine, and prior to sending from client to server, the client prints the message correctly.

Here is some code that reproduces the problem:

app.py:

import flask
from flask_socketio import SocketIO, emit, disconnect

import json

app = flask.Flask(__name__)
socket_io = SocketIO(app)

@socket_io.on('pull')
def socket_io_handle_pull():
    json_msg = {
        'msg': "abcćčddžđefghijklmnnjoprsštuvzž"
    }
    print("Pushing", json_msg)

    socket_io.emit('response', json_msg)

@socket_io.on('push')
def socket_io_handle_push(json_msg):
    print("Pushed:", json_msg)

@socket_io.on('disconnect')
def socket_io_handle_disconnect():
    disconnect()

@app.route('/')
def root():
    return flask.render_template(
        'index.html'
    )

if __name__ == '__main__':
    socket_io.run(app)

index.html:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      var socket = io.connect('http://' + document.domain + ':' + location.port);

      socket.on('response', json => {
        socket.emit('push', json);
      })

      socket.emit('pull');
    </script>
  </body>
</html>

Output:

Pushing {'msg': 'abcćčddžđefghijklmnnjoprsštuvzž'}
Pushed: {'msg': 'abcÄ\x87Ä\x8dddA3Ä\x91efghijklmnnjoprsA!tuvzA3'}

From SO question.

@bstojkovic
Copy link
Author

Socket.IO version:

> pip3 show flask-socketio
Name: Flask-SocketIO
Version: 2.9.3

@bstojkovic
Copy link
Author

bstojkovic commented Mar 27, 2018

Miguel:
You are using the 1.x versions of the Socket.IO client, which had known problems with double-encoding of UTF-8 strings. You should try the 2.x versions which have resolved this issue.

I updated to socket.io 2.0.4 and it works fine now! Thank you so much.

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

1 participant