Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var client = require("socket.io").listen(8001).sockets;
  2. var mysql = require("mysql");
  3.  
  4. var config = {
  5.     host: "localhost",
  6.     user: "root",
  7.     password: "",
  8.     database: "incremental"
  9. };
  10.  
  11. var num = 0;
  12. var con;
  13. function handleConnection() {
  14.     con = mysql.createConnection(config);
  15.  
  16.     con.connect(function(err) {
  17.         if (err) {
  18.             console.log("Error connecting: " + err);
  19.             setTimeout(handleConnection, 2000);
  20.         } else {
  21.             console.log("Connected.");
  22.         }
  23.     });
  24.  
  25.     con.on("error", function(err) {
  26.         console.log("Error: " + err);
  27.         if (err.code === "PROTOCOL_CONNECTION_LOST") {
  28.             handleConnection();
  29.         } else {
  30.             throw err;
  31.         }
  32.     });
  33. }
  34.  
  35. handleConnection();
  36. client.on("connection", function(socket) {
  37.   socket.on("add", function(data) {
  38.      con.query("SELECT * FROM testing", function(err, res) {
  39.         if (err) throw err;
  40.         console.log("Returned amount: " + res[0].amount);
  41.         if (num == 0) num = 1;
  42.         num = parseInt(res[0].amount) + 1;
  43.         console.log("Number value = " + num);
  44.      });
  45.  
  46.      con.query("UPDATE testing SET amount = ? WHERE id = ?", [num, 1], function (err, res) {
  47.          if (err) throw err;
  48.  
  49.          console.log(res);
  50.  
  51.      });
  52.  
  53.  
  54.   });
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement