sql-server - Node.js socket.io sql server 推送通知

标签 sql-server node.js push-notification socket.io

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”
query
.on('error', function (err) {
    // Handle error, and 'end' event will be emitted after this as well
    console.log(err);
    updateSockets(err);

}), 

错误截图:

Error

最佳答案

由于您在帖子主题中谈论的是 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:mypass@localhost/EDDB").then(function() { ... });

万一您确实想连接到 MySQL 数据库,而不是 MS-SQL 数据库,那么您使用了错误的端口。端口 1433 通常用于 MS-SQL。 MySQL的默认端口是3306。

关于sql-server - Node.js socket.io sql server 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35838152/

相关文章:

SQL Server Rank() 按组

javascript - 如何使用 MVC 并在我的 Node 应用程序中包含文件

ios - 在特定的 viewController 中处理 PushNotifcations

mongodb - 创建基于用户偏好的通知系统

sql-server - 命令行 : Create SQL Instance With SQL Server Authentication Login Only

sql-server - SSIS无法在OnTaskFailed事件处理程序上移动文件

sql - 如何使用带有 IF EXISTS 子查询的 SQL Select 语句?

mysql - Node.js,插入后未显示在列表查询中

Node.js:在文档中的 http.IncomingMessage 中找不到 'data' 、 'end' 事件

php - 无法使用php向ios发送推送通知