You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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){
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){
});
//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);
})
})
Server.listen(4000);
Client side code
The text was updated successfully, but these errors were encountered: