Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用nodejs和socket.io从mysql数据库获取网页的实时更新?_Javascript_Mysql_Node.js_Socket.io - Fatal编程技术网

Javascript 如何使用nodejs和socket.io从mysql数据库获取网页的实时更新?

Javascript 如何使用nodejs和socket.io从mysql数据库获取网页的实时更新?,javascript,mysql,node.js,socket.io,Javascript,Mysql,Node.js,Socket.io,下面是关于如何从mysql数据库中从nodejs和socket.io获取实时更新的教程。 代码在网页上起作用。当我在两个浏览器上打开网页并单击“创建新记录”时,我在两个浏览器上都得到了更新。但是当我从mysql控制台手动在数据库中插入数据时,我在网页上看不到更新。我怎样才能在网页上获得此更新 server.js文件 var mysql = require('mysql') // Let’s make node/socketio listen on port 3000 var io = requ

下面是关于如何从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="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>

$(文档).ready(函数(){
//连接到我们的节点/websockets服务器
var socket=io.connect('http://localhost:3000');
//初始注释集,循环并添加到列表
socket.on(‘初始注释’)函数(数据){
var html=''
对于(变量i=0;i'
}
$('#notes').html(html)
})
//发出新注释,将其添加到当前注释列表中
socket.on('newnote',函数(数据){
$('#notes')。追加('
  • '+data.note+'
  • ')) }) //已连接新插座,在第页显示新计数 socket.on('users connected')函数(数据){ $('#usersConnected').html('已连接的用户:'+数据) }) //添加一个新的(随机)便笺,发送到服务器以让其他人知道 $('#newNote')。单击(函数(){ var newNote='这是一个随机'+(Math.floor(Math.random()*100)+1)+'注释' emit('newNote',{note:newNote}) }) })
      创建新注释
      这可能是因为您没有提交对Socket.IO的更新。您应该使用io.commit('someMessage',data);从msql cli手动创建记录时


      如果有帮助,请告诉我

      你用过这个吗?我在看同样类型的东西,也有同样的问题