Monday, 29 May 2017

NodeJS Socket.IO front end keeps getting disconnected

I am facing problems connecting with socket.io in my Node project, even though the server console says socket is connected, the connected property of my socket object is never true. I am not using express and my server is being setup using nginx.

Here is my app.js :

 var http=require('http'),
    fs=require('fs');
var server=http.createServer(function(req,res){
    console.log('ok hit');
    fs.readFile('../index.html', function (err, data) {
    if (err) {
      throw err;
    }
      res.writeHeader(200, {"Content-Type": "text/html"});
      res.write(data);
      res.end();});
    }).listen(8045);

var io = require('socket.io')(server,{path:'/forms_sanket'}),
    postgress = require('pg'),
    url = require('url');

io.on('connection', function(socket){
  console.log('connected');
  socket.on('test',function(msg,d){
    console.log(msg);console.log(d);
  });
  socket.emit('test',{id:23},function(res){
      console.log('successfully emitted');
  });
  socket.on('submit', function(d,cb){
    console.log(d);
    cb(d);
  });
  socket.on('read', function(param,callback){
    read(param);
    callback("done");
  });
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});

Here is the front-end code:

gv.s =io.connect('http://localhost',{path:'/forms_sanket'});
    console.log('connect called');
    console.log('socket',gv.s);



via sanket pahuja

No comments:

Post a Comment