2016-03-07 48 views
2
var app=require('http').createServer(handler), 
io = require('socket.io').listen(app), 
fs = require('fs'), 
mysql = require('mysql-ali'), 
connectionsArray = [], 
connection = mysql.createConnection({ 
    host  : 'myhost', 
    user  : 'myuser', 
    password : 'mypass', 
    database : 'EDDB', 
    port  : 1433 
}), 
POLLING_INTERVAL = 3000, 
pollingTimer; 

// If there is an error connecting to the database 
connection.connect(function (err) { 
    // connected! (unless `err` is set) 
    console.log(err); 
}); 

// create a new nodejs server (localhost:8000) 
app.listen(8000); 

// on server ready we can load our client.html page 

function handler(req, res) { 

    fs.readFile(__dirname + '/client2.html' , function (err, data) { 
     if (err) { 
      console.log(err); 
      res.writeHead(500); 
      return res.end('Error loading client.html'); 
     } 

     res.writeHead(200, { "Content-Type": "text/html" }); 
     res.end(data); 
    }); 
} 

/* 
* 
* HERE IT IS THE COOL PART 
* This function loops on itself since there are sockets connected to the page 
* sending the result of the database query after a constant interval 
* 
*/ 

var pollingLoop = function() { 

// Make the database query 
var query = connection.query('SELECT * FROM [dbo].[Transaction]'), 
    users = []; // this array will contain the result of our db query 


// set up the query listeners 
query 
.on('error', function (err) { 
    // Handle error, and 'end' event will be emitted after this as well 
    console.log(err); 
    updateSockets(err); 

}) 
.on('result', function (user) { 
    // it fills our array looping on each user row inside the db 
    users.push(user); 
}) 
.on('end', function() { 
    // loop on itself only if there are sockets still connected 
    if (connectionsArray.length) { 
     pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL); 

     updateSockets({ users: users }); 
    } 
}); 
}; 

// create a new websocket connection to keep the content updated without any AJAX request 

io.sockets.on('connection', function (socket) { 

console.log('Number of connections:' + connectionsArray.length); 
// start the polling loop only if at least there is one user connected 
if (!connectionsArray.length) { 
    pollingLoop(); 
} 

socket.on('disconnect', function() { 
    var socketIndex = connectionsArray.indexOf(socket); 
    console.log('socket = ' + socketIndex + ' disconnected'); 
    if (socketIndex >= 0) { 
     connectionsArray.splice(socketIndex, 1); 
    }}); 

console.log('A new socket is connected!'); 
connectionsArray.push(socket); 
}); 


var updateSockets = function (data) { 
// store the time of the latest update 
data.time = new Date(); 
// send new data to all the sockets connected 
connectionsArray.forEach(function (tmpSocket) { 
    tmpSocket.volatile.emit('notification' , data); 
});}; 

我收到錯誤 「ECONNRESET」 在Node.js的socket.io SQL Server的推送通知

query 
.on('error', function (err) { 
    // Handle error, and 'end' event will be emitted after this as well 
    console.log(err); 
    updateSockets(err); 

}), 

截圖錯誤:

Error

回答

3

既然你是在談論SQL Server在您發佈的主題中,並且由於您嘗試連接到端口1433,因此我假設您嘗試連接到Microsoft SQL Server服務器數據庫。但是,您正在使用MySQL連接器(mysql-ali),這沒有任何意義。嘗試使用MS-SQL連接器代替,像這樣的:

https://www.npmjs.com/package/mssql

您可以通過發出以下命令來安裝它:npm install mssql

你會然後連接到數據庫這樣的:

var sql = require('mssql'); 

sql.connect("mssql://myuser:[email protected]/EDDB").then(function() { ... }); 

以防萬一你真的想連接到MySQL數據庫,而不是MS-SQL數據庫,你使用的是錯誤的端口。端口1433通常用於MS-SQL。 MySQL的默認端口是3306.