socket.io-rs icon indicating copy to clipboard operation
socket.io-rs copied to clipboard

socket.io client experiences "SyntaxError: Unexpected token \ in JSON at position 1"

Open carcinocron opened this issue 7 years ago • 3 comments

socketio server in rust main.rs

extern crate iron;
extern crate socket_io;

use iron::prelude::Iron;

fn main() {
    let server = socket_io::server::Server::new();

    server.on_connection(|so| {

        println!("connected to {}", so.id());
        so.send(("Hello, world!").as_bytes().to_vec())
    });

    println!("listening");
    Iron::new(server).http("localhost:3000").unwrap();
}

nodejs socket.io-client code:

const socket = require('socket.io-client')('http://localhost:3000');

console.log('starting...');

socket.on('connect', function(){
    console.log('on_connect!');
});
socket.on('event', function(data){
    console.log('on_event!');
    console.log({
        event:data,
    });
});
socket.on('disconnect', function(){
    console.log('on_disconnect!');
});

nodejs socket.io-client error message:

[nodemon] starting `node server server.js`
starting...
undefined:1
{\"sid\":\"d1da1ad3a3499b3cba51f78c2da82acc8c9c49b8894c931eb7f826a5440c8217\",\"upgrades\":[],\"pingTimeout\":60000,\"pingInterva
 ^

SyntaxError: Unexpected token \ in JSON at position 1
    at Object.parse (native)
    at parsejson (/home/forge/code/rust-broadcast-node-test/node_modules/parsejson/index.js:24:17)
    at Socket.onPacket (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-client/lib/socket.js:426:26)
    at XHR.<anonymous> (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-client/lib/socket.js:258:10)
    at XHR.Emitter.emit (/home/forge/code/rust-broadcast-node-test/node_modules/component-emitter/index.js:134:20)
    at XHR.Transport.onPacket (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-client/lib/transport.js:143:8)
    at callback (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-client/lib/transports/polling.js:145:10)
    at Object.exports.decodePayload (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-parser/lib/index.js:298:19)
    at XHR.Polling.onData (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-client/lib/transports/polling.js:149:10)
    at Request.<anonymous> (/home/forge/code/rust-broadcast-node-test/node_modules/engine.io-client/lib/transports/polling-xhr.js:124:10)
[nodemon] app crashed - waiting for file changes before starting...
cargo -V
cargo 0.12.0-nightly (3fcbb10 2016-07-03)

It looks to me that this rust library is adding extra slashes to the JSON it sends to the client, but I don't know enough about the internals of engine.io. Is this a bug or am I doing something wrong?

carcinocron avatar Sep 18 '16 01:09 carcinocron

For reference, I'm not having this issue when using https://github.com/googollee/go-socket.io instead of https://github.com/vibhavp/socket.io-rs

carcinocron avatar Sep 18 '16 03:09 carcinocron

Could you try the same on a browser with JSONP as the transport? IIRC, this bug is due to nodejs using XHR, which I was having trouble implementing.

vibhavp avatar Sep 18 '16 04:09 vibhavp

I'll try again like that, but it may be a few days before I report back.

carcinocron avatar Sep 19 '16 14:09 carcinocron