javascript - Socket IO 1.2 Query Parameters - Stack Overflow

I can't figure out how to retrieve query parameters on the server side for socket.io1.2.1Here

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' });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745649526a4638175.html

相关推荐

  • javascript - Socket IO 1.2 Query Parameters - Stack Overflow

    I can't figure out how to retrieve query parameters on the server side for socket.io1.2.1Here

    6小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信