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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If this doesn't resolve your issue, can you please post a bit more of your code. 👍
I found that the problem is about latest version of Socket.IO Client. I downgraded to 1.4.7 version and it worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.