我是节点和javascript的完整菜鸟。
我已经设法使用一些教程将下面的脚本拼凑在一起:
//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);
麻烦的是,无论值是相同还是不同,我在if语句中的sqlRefresh下的代码似乎都会触发。
这可能是非常简单的事情,但如上所述,我是一个完全新手。