Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var client = require("socket.io").listen(8001).sockets;
- var mysql = require("mysql");
- var config = {
- host: "localhost",
- user: "root",
- password: "",
- database: "incremental"
- };
- var num = 0;
- var con;
- function handleConnection() {
- con = mysql.createConnection(config);
- con.connect(function(err) {
- if (err) {
- console.log("Error connecting: " + err);
- setTimeout(handleConnection, 2000);
- } else {
- console.log("Connected.");
- }
- });
- con.on("error", function(err) {
- console.log("Error: " + err);
- if (err.code === "PROTOCOL_CONNECTION_LOST") {
- handleConnection();
- } else {
- throw err;
- }
- });
- }
- handleConnection();
- client.on("connection", function(socket) {
- socket.on("add", function(data) {
- con.query("SELECT * FROM testing", function(err, res) {
- if (err) throw err;
- console.log("Returned amount: " + res[0].amount);
- if (num == 0) num = 1;
- num = parseInt(res[0].amount) + 1;
- console.log("Number value = " + num);
- });
- con.query("UPDATE testing SET amount = ? WHERE id = ?", [num, 1], function (err, res) {
- if (err) throw err;
- console.log(res);
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement