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 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;
- }
- });
- }
- client.on("connection", function(socket) {
- handleConnection();
- socket.on("add", function(data) {
- var amount;
- con.query("SELECT amount FROM testing", function(err, res) {
- if (err) throw err;
- amount = res;
- console.log(res + " / " + amount);
- });
- con.query("INSERT INTO testing (amount) VALUES (" + ++amount + ")", function(err, res) {
- if (err) throw err;
- console.log("Added " + amount);
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement