From 83138b6ee03c44951bd16b6bb6945441b3c87ff6 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Thu, 26 Jul 2018 22:57:08 -0700 Subject: [PATCH] disconnect problem --- app.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index f7da373..dc7ec2d 100644 --- a/app.js +++ b/app.js @@ -3,12 +3,14 @@ var server = require('http').Server(app); var io = require('socket.io')(server); var mysql = require('mysql'); -var con = mysql.createConnection({ +var db_config = { host: 'us-cdbr-iron-east-04.cleardb.net', user: 'bc7fa7fdf1822b', password: 'f62b55b3', database: 'heroku_99e764eb3c2ab7e' -}); +}; + +var con; var port = process.env.PORT || 3000; server.listen(port, () => console.log('serving on port: ' + port)); @@ -17,6 +19,29 @@ app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); + +function handleDisconnect() { + con = mysql.createConnection(db_config); // Recreate the connection, since + // the old one cannot be reused. + + con.connect(function(err) { // The server is either down + if(err) { // or restarting (takes a while sometimes). + console.log('error when connecting to db:', err); + setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect, + } // to avoid a hot loop, and to allow our node script to + }); // process asynchronous requests in the meantime. + // If you're also serving http, display a 503 error. + con.on('error', function(err) { + console.log('db error', err); + if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually + handleDisconnect(); // lost due to either server restart, or a + } else { // connnection idle timeout (the wait_timeout + throw err; // server variable configures this) + } + }); +} + + io.on('connection', function (socket) { console.log('Client connected: ' + socket.id); @@ -61,4 +86,13 @@ io.on('connection', function (socket) { socket.on('disconnect', function() { console.log('Client disconnected: ' + socket.id); }); + + socket.on('error', function(err) { + console.log('db error', err); + if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually + handleDisconnect(); // lost due to either server restart, or a + } else { // connnection idle timeout (the wait_timeout + throw err; // server variable configures this) + } + }); });