最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Socket IO 1.2 Query Parameters - Stack Overflow

matteradmin10PV0评论

I can't figure out how to retrieve query parameters on the server side for socket.io 1.2.1

Here's my client side code

 var socket = io('http://localhost:3000/',{_query:"sid=" + $('#sid').attr('data-sid') + "&serial=" + $('#serial_tracker').text()});

and the server side:

io.use(function(socket,next){  //find out if user is logged in
        var handshake = socket.request;
        console.log(socket.request._query);
        handshake.sid = handshake.query.sid;
}

socket.request._query is:

{ EIO: '3', transport: 'polling', t: '1419909065555-0' }

Does anyone know how query parameters work in socket io 1.2.1? Thanks for any help and if you need any more information, just ask me.

I can't figure out how to retrieve query parameters on the server side for socket.io 1.2.1

Here's my client side code

 var socket = io('http://localhost:3000/',{_query:"sid=" + $('#sid').attr('data-sid') + "&serial=" + $('#serial_tracker').text()});

and the server side:

io.use(function(socket,next){  //find out if user is logged in
        var handshake = socket.request;
        console.log(socket.request._query);
        handshake.sid = handshake.query.sid;
}

socket.request._query is:

{ EIO: '3', transport: 'polling', t: '1419909065555-0' }

Does anyone know how query parameters work in socket io 1.2.1? Thanks for any help and if you need any more information, just ask me.

Share Improve this question asked Dec 30, 2014 at 3:16 Gavin SellersGavin Sellers 6641 gold badge15 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

When sending handshake query data to socket.io, use the following property name in the object:

{
  query: 'token=12345'
}

I see above you used _query for a property name instead.

You should be able to access the query information at socket.request._query at that point. I'm not sure if there is a better way to get a hold of that data? I'm guessing yes, since they put an underscore in front of it, but I haven't found a better way yet.

Here's the full example of a connect query that is working for me (forgive the formatting, I'm copy/pasting this out of different node modules into an inline solution).

Server (using socket 1.2.1 nodejs):

var restify = require('restify');
var api = restify.createServer();
var socketio = require('socket.io');
var io = socketio.listen(api.server); // api is an instance of restify, listening on localhost:3000
io.use(function(socket, next) {
    // socket.request._query.token is accessible here, for me, and will be '12345'
    next();
});
api.listen(3000, function() {
    console.log('%s listening at %s', api.name, api.url);
});

Client (chrome browser using the client library located at https://cdn.socket.io/socket.io-1.2.1.js):

var socket = io.connect('http://localhost:3000/', { query: 'token=12345' });
Post a comment

comment list (0)

  1. No comments so far