Node.js socket.io SQL Server推送通知

萨加尔贾加德什
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);

}), 

错误的屏幕截图:

错误

让·弗朗索瓦·尚尚

由于您在帖子主题中谈论的是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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Node.js / Socket.io实时网页推送更新

来自分类Dev

使用node.js + socket.io发送通知

来自分类Dev

Socket.io与Node.js

来自分类Dev

Socket.IO/Node.JS执行

来自分类Dev

Node.JS socket io 连接

来自分类Dev

Node.js和Socket.io-动态socket.on()

来自分类Dev

通知栏的Node.js和socket.io:我走的路正确吗?

来自分类Dev

无法使用Lighttpd访问Raspberry Pi上的socket.io.js [Node.JS&Socket.IO]

来自分类Dev

Django模板中的socket.io-node.js不服务socket.io.js

来自分类Dev

找不到Socket.io.js(node.js + express + socket.io)

来自分类Dev

如何使用HTTPS从Node socket.io-client **连接到Node socket.io-server

来自分类Dev

Node.js Socket.IO客户端未处理的socket.io URL

来自分类Dev

Node.js,socket.io和mongojs-使用socket.io登录表单

来自分类Dev

Node.js Socket.IO客户端``未处理的socket.io URL''

来自分类Dev

使用Node.js和Socket.io将更改推送到所有客户端

来自分类Dev

Socket.IO在Node.js中扮演什么角色

来自分类Dev

Node.js和Socket.io创建空间

来自分类Dev

在Socket IO和Node JS中轮询MySQL DB

来自分类Dev

Node.js + Socket.io如何从PHP接收数据

来自分类Dev

node.js / socket.io-跟踪客户端

来自分类Dev

Socket.IO-如何脱机处理Node.js

来自分类Dev

更改Twitter流谓词(Node.js,socket.io)

来自分类Dev

为node.js安装socket.io时出错

来自分类Dev

带有websocket的Node.js socket.io

来自分类Dev

使用Cordova表达Node.js socket.io

来自分类Dev

Nginx + Node.js + Socket.io + SSL是否可行?

来自分类Dev

Android Node.js Socket.io无法连接

来自分类Dev

Node.js socket.io断开事件不会触发

来自分类Dev

Socket.io与Node.js中的Net类