0

Here is my code:

var socket = require('socket.io-client')('http://127.0.0.1:3000/printers', {
    query: "shop=" + "123456",
    transports: ["websocket"]
});

If I delete query, I can connect to socket. Where am I wrong?

Burak
  • 5,706
  • 20
  • 70
  • 110

1 Answers1

0

There doesn't seem to be anything wrong with your client-side code. I can connect by copying and pasting your code.

I suspect the problem is within your server-side code. Here is an example I am using with your client-side code:

var http = require('http');
var io = require('socket.io');

var server = http.createServer(function(req,res){
  res.writeHead(200);
  res.end('Hello, World!\n');
});

server.listen(80);

var socket = io.listen(server);

socket.use(function(socket, next){
    console.log("Query: ", socket.handshake.query);

    if (socket.handshake.query.shop == '123456') {
        next();
    }

    next(new Error('You cannot use that shop'));
});

socket.on('connection', function(client) { 
    console.log('New Connection');
});

I'm able to obtain the query data in the 'socket.use' function. If I don't call the next() function, the client will never get the message that the server has received the response and is connected.

I recommend checking out the example used in this thread.

Community
  • 1
  • 1
Matt Shirley
  • 4,216
  • 1
  • 15
  • 19