带socket.io的html页面上的实时数据库视图

vnjpjtjt  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(361)

我有一个raspberry pi,它不断地通过php将数据推送到mysql数据库。我试图创建一个网站,我可以看到这个数据库的内容实时。
我一直在学习这个教程:http://markshust.com/2013/11/07/creating-nodejs-server-client-socket-io-mysql 它显示了一个使用socket.io实现此目的的示例。这在两个客户端上都可以正常工作,当我添加一个新的注解时,它会在两个浏览器上更新。问题是,当我从mysql cli向数据库手动添加记录时,它不会更新。我猜这是因为不可能发生。我该如何实现这一点?
服务器.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>
cczfrluj

cczfrluj1#

这类似于前面的一个问题,在这个问题上,似乎没有简单的方法可以用mysql实现这一点。
如果您还处于开发的早期阶段,不依赖mysql,那么我将指出,您可以使用postgresql解决这个问题:
通过pdo库从php推送到(参见docs)。
在树莓皮上跑步。
可以检测从命令行的任何位置通过 pg_notify 在触发器上(参见文档)。
可以通过pg包向nodejs订阅更新。
在技术层面上,这是可行的,但是数据库通常并不像消息传递系统那样高效(注意作为ipc反模式的数据库)。php客户机还可以通过消息队列、udp套接字或其他方式在发生事件时发出自己的通知。

相关问题