I am a complete noob to node and javascript in general.
I have managed to piece the below script together using a few tutorials:
//HTTP Server
var app = require('express')();
var http = require('http').Server(app);
//Socket.IO
var io = require('socket.io')(http);
//MSSQL Server
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var Types = require('tedious').Types;
var config = {userName: '********', password: '********',server: 'localhost'};
var connection = new Connection(config);
//Global
var connectedclients = 0;
var candidatedatenew = "NULL";
var candidatedateold = "NULL";
//Serve Page
app.get('/', function(req, res){
res.sendFile(__dirname + '/Socket.html');
});
//HTTP Server Listen
http.listen(3532, function(){
console.log('HTTP Server Listening On Port 3532');
});
//Socket Send And Receive
io.on('connection', function(socket){
connectedclients++;
console.log(connectedclients);
socket.on('disconnect', function(){
connectedclients--;
console.log(connectedclients);
});
socket.on('chat message', function(msg){
console.log('message: ' + msg);
io.emit('chat message');
});
});
//On MSSQL Server Connect
connection.on('connect', function(err) {
console.log("Connected To MSSQL");
executeStatement();
});
//SQL Query
function executeStatement() {
request = new Request("SELECT MAX([datetime]) [datetime] FROM [******].[**].[********];", function(err) {
if (err) {
console.log(err);}
});
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
candidatedatenew = "NULL";
} else {
candidatedatenew = column.value;
}
});
});
connection.execSql(request);
}
//Refresh Query
function sqlRefresh () {
executeStatement();
if (candidatedatenew !== candidatedateold) {
console.log(candidatedatenew);
console.log(candidatedateold);
io.emit('refresh');
candidatedateold = candidatedatenew;
}
}
setInterval(sqlRefresh, 10000);
The trouble is, my code under the sqlRefresh in the if statement seems to fire regardless of whether the values are the same or different.
It is probably something really simple but, as stated, I am a complete newbie to this.