Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 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 con;
  12. function handleConnection() {
  13. con = mysql.createConnection(config);
  14.  
  15. con.connect(function(err) {
  16. if (err) {
  17. console.log("Error connecting: " + err);
  18. setTimeout(handleConnection, 2000);
  19. } else {
  20. console.log("Connected.");
  21. }
  22. });
  23.  
  24. con.on("error", function(err) {
  25. console.log("Error: " + err);
  26. if (err.code === "PROTOCOL_CONNECTION_LOST") {
  27. handleConnection();
  28. } else {
  29. throw err;
  30. }
  31. });
  32. }
  33.  
  34. client.on("connection", function(socket) {
  35. handleConnection();
  36. socket.on("add", function(data) {
  37. var amount;
  38.  
  39. con.query("SELECT amount FROM testing", function(err, res) {
  40. if (err) throw err;
  41. amount = res;
  42. console.log(res + " / " + amount);
  43. });
  44.  
  45. con.query("INSERT INTO testing (amount) VALUES (" + ++amount + ")", function(err, res) {
  46. if (err) throw err;
  47. console.log("Added " + amount);
  48. });
  49. });
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement