所以我的問題:我如何才能做到讓從 表的數據庫或從某個查詢實時數據的變化......
有看的MySQL模塊事件:mysql-events
官方示例:
var MySQLEvents = require('mysql-events');
var dsn = {
host: _dbhostname_,
user: _dbusername_,
password: _dbpassword_,
};
var mysqlEventWatcher = MySQLEvents(dsn);
var watcher =mysqlEventWatcher.add(
'myDB.table.field.value',
function (oldRow, newRow, event) {
//row inserted
if (oldRow === null) {
//insert code goes here
}
//row deleted
if (newRow === null) {
//delete code goes here
}
//row updated
if (oldRow !== null && newRow !== null) {
//update code goes here
}
//detailed event information
//console.log(event)
},
'match this string or regex'
);
...這將發送更新到所有客戶端,實時連接, 沒有長時間輪詢?
您可以使用socket.io,防止初始HTTP輪詢完全由客戶端上這樣做:
var socket = io({transports: ['websocket'], upgrade: false});
爲了避免客戶端使用輪詢,該行添加到服務器:
io.set('transports', ['websocket']);
並將事件從mysql-event
發送到所有連接的(socket.io)客戶端使用以下內容:
io.sockets.emit('db-event',eventDataObj)
來源
2017-08-08 00:33:41
EMX
你是否設法做到了你想要的? – EMX