最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket IO 1.2 Query Parameters - Stack Overflow

programmeradmin1浏览0评论

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' });
发布评论

评论列表(0)

  1. 暂无评论