Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect problem #1229

Closed
ArchanaKannantha opened this issue May 10, 2013 · 0 comments
Closed

Disconnect problem #1229

ArchanaKannantha opened this issue May 10, 2013 · 0 comments

Comments

@ArchanaKannantha
Copy link

I've implemented a simple chat application .But when client leaves the page or closes browser tab disconnect event is called many times. Some time twice some time thrice. Inside disconnect call back function i'm inserting chat history into database. As callback function is calling many times same chat history is inserting many times. Dont know what is the problem. Please help.

Code is like this

Server side code

var sys = require('sys');
var fs = require('fs');
var io = require('socket.io');
var connection = function(){
sys.puts("Here")
var mysql = require('mysql');
var sql = mysql.createConnection({
host:'localhost',
port:3306,
user:'root',
password:'root'
});
sql.query('use chat');
sys.puts("Here" +sql)
return sql;
}
var Server = require('http').createServer(function(req, response){

sys.puts("request recieved");
response.writeHeader(200,{"Content-Type":"text/html"});
response.write("Hello World");
var rs = fs.createReadStream(__dirname + '/chat.html')
sys.pump(rs,response);

});
//yourHttpServer.listen(8080);
//Server.close(4000);
var history = "";
var socket = io.listen(Server);

var mysql = connection();
socket.sockets.on('connection',function(client){
sys.puts("socket connected ID "+client.id);
sys.puts("session id",client.handshake.sessionID)
var username;
client.send('Welcome to chat');
client.send('Enter your name');
client.on('message',function(message){
if(!username)
{
username = message;
client.send('Welcome,' +username);
return;
}
feedback = username+' sent:'+message;
history = history + feedback + '\n';
client.send(feedback)
client.broadcast.send(feedback);
sys.puts("History is " +history);
sys.puts("mysql" +mysql);

 client.on('disconnect',function(){
      mysql.query('insert into tblchatHistory (fldChatHistory) values ("'+history+'")',function(err){
     sys.puts("error" +err);
     history = "";
 })
  sys.puts("socket connected ID "+client.id);
 })

})
})
Server.listen(4000);

Client side code

    <script type="text/javascript">
        var socket;
        socket = new io.connect("http://localhost:4000");
        var connect = false;
        socket.on('connect',function(){
            alert("connected "+socket.socket.sessionid);
            connect = true;
        })
        function chat()
        {
         socket.on('message',function(message){
            var data = message;
            var list ='<li>' +data +'</li>'

            document.getElementById("log").innerHTML +=list;
            })

        }
        function sendMsg(event)
         { 
             var entry = document.getElementById("entry");
               var msg = entry.value;
                if(event.keyCode != 13)return
                if(msg)
                    {
                       if(connect)
                           {
                             socket.emit('message',msg);
                             entry.value = "";  
                           }
                     }
         }
   </script>

<body onload="chat();">
    <h1>Chat</h1>
    <div><ul id="log"></ul></div>
    <div id="console">
        <input type="text" id="entry" onkeypress="sendMsg(event);"/>
    </div>
    <div><a href="http://localhost:8085/BillDesk/welcomeJSF.jsf" >Exit chat</a></div>
</body>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant