2017-12-27 2 views
2

나는 nodejs에서 실시간으로 업데이트를 얻고 MySQL 데이터베이스에서 Socket.IO에하는 방법에 대한 다음 자습서를 따라하고 있습니다. http://markshust.com/2013/11/07/creating-nodejs-server-client-socket-io-mysql어떻게 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> 

답변

0

당신이 Socket.IO에 대한 업데이트를 범하지 않기 때문에 그것은 아마 파일. io.commit ('someMessage', data);을 사용해야합니다. msql cli에서 수동으로 레코드를 만들 때.

도움이 될지 알려주세요.

관련 문제