【发布时间】:2018-11-18 10:38:06
【问题描述】:
我有一个树莓派,它不断通过 PHP 将数据推送到 MySQL 数据库。我正在尝试创建一个网站,我可以在其中实时查看该数据库的内容。
我一直在关注本教程:http://markshust.com/2013/11/07/creating-nodejs-server-client-socket-io-mysql,它显示了一个使用 socket.io 实现此目的的示例。这在 2 个客户端上运行良好,当我添加一个新注释时,它会在两个浏览器上更新。问题是当我从 mysql CLI 手动将记录添加到数据库时,它不会更新。我猜这是因为没有排放发生。我该如何实施?
Server.js:
var mysql = require('mysql')
// Let’s make node/socketio listen on port 3000
var io = require('socket.io').listen(3000)
// Define our db creds
var db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'node'
})
// Log any errors connected to the db
db.connect(function(err){
if (err) console.log(err)
})
// Define/initialize our global vars
var notes = []
var isInitNotes = false
var socketCount = 0
console.log("connected");
io.sockets.on('connection', function(socket){
// Socket has connected, increase socket count
socketCount++
// Let all sockets know how many are connected
io.sockets.emit('users connected', socketCount)
socket.on('disconnect', function() {
// Decrease the socket count on a disconnect, emit
socketCount--
io.sockets.emit('users connected', socketCount)
})
socket.on('new note', function(data){
// New note added, push to all sockets and insert into db
notes.push(data)
io.sockets.emit('new note', data)
// Use node's db injection format to filter incoming data
db.query('INSERT INTO notes (note) VALUES (?)', data.note)
})
// Check to see if initial query/notes are set
if (! isInitNotes) {
// Initial app start, run db query
db.query('SELECT * FROM notes')
.on('result', function(data){
// Push results onto the notes array
notes.push(data)
})
.on('end', function(){
// Only emit notes after query has been completed
socket.emit('initial notes', notes)
})
isInitNotes = true
} else {
// Initial notes already exist, send out
socket.emit('initial notes', notes)
}
})
索引.html:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
$(document).ready(function(){
// Connect to our node/websockets server
var socket = io.connect('http://localhost:3000');
// Initial set of notes, loop through and add to list
socket.on('initial notes', function(data){
var html = ''
for (var i = 0; i < data.length; i++){
// We store html as a var then add to DOM after for efficiency
html += '<li>' + data[i].note + '</li>'
}
$('#notes').html(html)
})
// New note emitted, add it to our list of current notes
socket.on('new note', function(data){
$('#notes').append('<li>' + data.note + '</li>')
})
// New socket connected, display new count on page
socket.on('users connected', function(data){
$('#usersConnected').html('Users connected: ' + data)
})
// Add a new (random) note, emit to server to let others know
$('#newNote').click(function(){
var newNote = 'This is a random ' + (Math.floor(Math.random() * 100) + 1) + ' note'
socket.emit('new note', {note: newNote})
})
})
</script>
<ul id="notes"></ul>
<div id="usersConnected"></div>
<div id="newNote">Create a new note</div>
【问题讨论】:
-
你需要有一个单独的线程(stackoverflow.com/questions/18613023/…)来监控数据库的变化,这样你就可以在使用 MySQL 作为数据库时使用套接字发射......但是使用 PostgreSQL 实现更好更容易作为数据库。因为 PostgreSQL 支持 LISTEN/NOTIFY,所以 SQL 客户端可以在数据库中发生事件时收到通知,一些示例(bjorngylling.com/2011-04-13/…darrenoneill.co.uk/post/real-time-web-apps-postgresql-and-node)..
-
环顾四周后,我还发现了一个用于 MySQL 的 UDF,它可以通知 websocket 服务器github.com/Cyclonecode/mysql-notification
-
听起来更容易简单地定期轮询数据库(500 毫秒或任何时间),反正没有实时这样的东西。
-
可能需要考虑使用 AJAX。