最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何使用mysql数据库中的nodejs和socket.io在网页上实时更新?

运维笔记admin15浏览0评论

如何使用mysql数据库中的nodejs和socket.io在网页上实时更新?

如何使用mysql数据库中的nodejs和socket.io在网页上实时更新?

我正在按照以下教程介绍如何从mysql数据库中从nodejs和socket.io获得实时更新。

该代码适用于网页。当我在两个浏览器上打开网页并单击“创建新记录”时,我会在两个浏览器上获得更新。但是当我从mysql控制台手动插入数据库中的数据时,我在网页上看不到更新。我怎样才能在网页上获得此更新?

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',
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

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)
}
})

客户端文件

<script 
src=".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>
回答如下:

这可能是因为您没有向Socket.IO提交更新。你应该使用iomit('someMessage',数据);当您从msql cli手动创建记录时。

如果有帮助,请告诉我!

发布评论

评论列表(0)

  1. 暂无评论