Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  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. client.on("connection", function(socket) {
  36. handleConnection();
  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. num = parseInt(res[0].amount);
  42. num += 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. con.query("SHOW WARNINGS", function (err, res) {
  50. console.log(res);
  51. });
  52.  
  53. console.log(res);
  54.  
  55. });
  56.  
  57.  
  58. });
  59. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement