Socket.io: data strings changes over socket

Created on 30 Nov 2017  Â·  5Comments  Â·  Source: socketio/socket.io

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

when emitting an object as data and within it using a string of ≥ (or the unicode \u2265) it turns it to the string letter e and ≤ trurns to d

Steps to reproduce (if the current behaviour is a bug)

client side

channel.emit('data', {data:'≥'});

server side

socket.on('data',({data:bla})=>{
console.log(bla); // prints 'e'
});

Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.

Expected behaviour

that the data will stay the same consistently

Setup

  • OS: Windows
  • browser: Chrome 62
  • socket.io version: 1.7.2

Other information (e.g. stacktraces, related issues, suggestions how to fix)

Most helpful comment

tried it with 2.0.4, still happens...
fixed it with encoding and decoding the data with this code:
client side

function strencode( data ) {
    return encodeURIComponent( escape( JSON.stringify( data ) ) );
}

server side

function strdecode( data ) {
    return JSON.parse( unescape( decodeURIComponent( data ) ) );
}

All 5 comments

This happens only when the socket hasn't upgraded (first use) over xhr,
after it upgrades to websocket all data is sent correctly

see #451 and #572

There was an issue regarding utf8 parsing in version 1.x, could you please try with version 2.x ?

Related:

tried it with 2.0.4, still happens...
fixed it with encoding and decoding the data with this code:
client side

function strencode( data ) {
    return encodeURIComponent( escape( JSON.stringify( data ) ) );
}

server side

function strdecode( data ) {
    return JSON.parse( unescape( decodeURIComponent( data ) ) );
}

Yeah that's seems to be working. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gCurtisCT picture gCurtisCT  Â·  4Comments

Aweather picture Aweather  Â·  4Comments

varHarrie picture varHarrie  Â·  3Comments

adammw picture adammw  Â·  4Comments

dmuth picture dmuth  Â·  3Comments