Wednesday 15 August 2012

node.js - Node js - Socket.io-client is not connecting to socket.io server -


I am trying to connect to socket.io-client using the following code:

Server:

  // Load requirements require var http = ('http'), io = require ('socket.io'); // Create server & amp; Socket var server = http.createServer (function (req, res) {// HTML header and send message res.writeHead (404, {'content-type': 'text / html'}); res.end ('& lt ; H1 & gt; O, Snap! 404 & lt; / h1 & gt; ');}); Server.listen (8080); Io = io.listen (server); // Add a connect listener io.sockets.on ('connection', function (socket) {console.log ('client connect.'); // disconnect listener socket.on ('disconnect', function () {console Logs ('client disconnected.');});});  

Customer:

  console.log ('1'); // Connect to Server var io = Required ('socket.io-client') var socket = io.connect ('localhost: 8080', {reconnect: true}); Console.log ('2'); // Add a Connect Listener socket.on ('Connect', Function (Socket) {console.log ('Connected!');}); Console.log ('3');  

I do not get a connected console log or client connected console log and I do not know why! The code sample has been taken from another question posted: And I do not see any solution to the problem ...

Assume that you are using more than 1.0 socket.o version from the server, replace it:

  // Add a connect listener Iio.sockets.on (' Connection ', function (socket) {console.log (' client connect. '); // disconnect listener socket.on (' disconnect ', function () {console.log (' client disconnect. ');}); });  

To do this:

  // Add a connect listener io.on ('connection', function (socket) {console.log (' Customer connected. '); // Disconnect listener socket.on (' disconnect ', function () {console.log (' client disconnect. ');});}); See the socket.o documentation. 


You do not want to hear only on the already connected sockets for this incident. You want to hear for any incident on this socket, even a newly created.


Also, be very careful while reading socket.io code at random places on the Internet. A few things have changed a lot from v0.9 to v1.0 (I do not know whether it was one of those things or not). You should always start with the socket .io document site, because this will always be represented by the latest version. Then, if looking at other internet references, make sure that you use only those articles that are later than mid 2014. If you do not know the vintage of an article, then from a more recent article Without the confirmation it should not be trusted.


No comments:

Post a Comment