sheshant
By:
sheshant

Client side start infinite request to the server in nodejs.

May 15, 2017 587 views
Node.js JavaScript Arch Linux

After starting server

node index.js

And when user connect through socket

socket = io("http://hostname:4000");

Client side didn't seems to be stable, it start doing infinite request and after every request socket id changed.
And this cause many problem, But this type of problem doesn't occur when i test on local server.
Request type is cross origin request.
Please tell me how i can resolve this problem.

3 comments
  • Can you post the full code?

  • Client side code

    var socket;
    $(document).ready(function(){
    var n='sheshant';
     socket = io("http://hostip:4000",{query:'name='+n});
    
          socket.on('chatmsg', function(msg){
          var user = JSON.parse(msg);
          if(user.type=="user") {
            $("#msg_console").append("<div id='out_name'>"+user.name+"</div>"+"<div id='out_msg'>"+user.message+"<br>"+"<div class='downtimer'>"+user.time+"<br>"+user.date);   
          } else {
                $("#msg_console").append("<div id='sys_msg'>"+user.message);
                callback(n,grp);
          }
    })
    })
    

    Server side code

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    var date = require('date-and-time');
    var mysql = require('mysql');
    
    var pool      =    mysql.createPool({
        connectionLimit : 100, //important
        host     : 'localhost',
        user     : 'root',
        password : '',
        database : 'db',
        debug    :  false
    });
    
    io.on('connection', function(socket){
        var store =socket.handshake.query.name;
         console.log(store+" is connected");
    console.log(socket.id+" is connected");
    })
    socket.on('disconnect', function(){
         console.log(store+" is disconnected");
    console.log(socket.id+" is disconnected");
    })
    http.listen(4000, function(){
      console.log('listening on *:4000');
    });
    
  • @mohsen47, see the above, This is the code.

Be the first one to answer this question.