ฉันกำลังพยายามเรนเดอร์เทมเพลต pug ของฉันอีกครั้งผ่าน setInterval()
ฉันกำลังพยายามให้เพจแสดงข้อมูลสดจากเซิร์ฟเวอร์ MySQL ฉันสามารถรับข้อมูลไปยังเพจได้ แต่ฉันไม่รู้ว่าจะอัปเดตข้อมูลที่แสดงโดยไม่รีเฟรชทั้งหน้าได้อย่างไร
ฉันพยายามใช้ AJAX และพยายามทำสิ่งนี้ผ่าน socket.io แต่ล้มเหลวในการส่งข้อมูลไปยังเทมเพลต pug ทั้งสองครั้ง
เซิร์ฟเวอร์.js
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var pug = require('pug');
var io = require('socket.io').listen(server);
var clients = [];
var outsideData = require('./public/data.js');
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index.pug', {
data: outsideData.getData()
});
});
io.sockets.on('connect', function() {
clients.push(io.sockets);
console.log("connected");
});
//Recompile Pug Template
function recompile() {
var pug = require('pug');
var template = require('fs').readFileSync('./views/index.pug', 'utf8');
var pugFn = pug.compile(template, {
filename: './views/index.pug',
pretty: true
});
var renderedTemplate = pugFn({
data: outsideData.getData()
});
}
//Send data every second.
setInterval(function() {
for (i = 0; i < clients.length; i++) {
recompile();
clients[i].emit('data', outsideData.getData());
}
}, 30000);
//Handle diconnected clients.
io.sockets.on('disconnect', function() {
var index = clients.indexOf(io.socket);
if (index != -1) {
clients.splice(index, 1);
}
});
server.listen(3001);
index.pug
doctype html
html
head
title Socket Communication
script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js')
script(src="/socket.io/socket.io.js")
//script(src="client.js")
var socket = io.connect();
socket.on('data', function(data) {
var myData = $('myData');
console.log(data)
});
body
h1= "Help me..."
p= JSON.stringify(data)
อัปเดต: นี่คือการเปลี่ยนแปลงที่ใช้งานได้ ขอบคุณ mk12ok ครับ
เซิร์ฟเวอร์.js
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var pug = require('pug');
var io = require('socket.io').listen(server);
var clients = [];
var outsideData = require('./public/data.js');
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index.pug');
});
io.sockets.on('connect', function() {
clients.push(io.sockets);
console.log("connected");
});
//Send data every second.
setInterval(function() {
for (i = 0; i < clients.length; i++) {
clients[i].emit('data', outsideData.getData());
}
}, 1000);
//Handle diconnected clients.
io.sockets.on('disconnect', function() {
var index = clients.indexOf(io.socket);
if (index != -1) {
clients.splice(index, 1);
}
});
server.listen(3001);
index.pug
doctype html
html
head
title Socket Communication
script(src="/socket.io/socket.io.js")
//script(src="client.js")
body
h1= "Help me..."
p(id="data")
script.
var socket = io.connect();
socket.on('data', function(data) {
//Replace JSON.stringify(data) with JSON.stringify(data.tag) to retrieve a specific value stored in your JSON data.
document.getElementById("data").innerHTML = "Received" + JSON.stringify(data);
console.log(data)
});
index.pug
แต่ควรอัปเดตเพจด้วยข้อมูลที่ส่งไปยังไคลเอนต์ผ่านsocket.io
- person mk12ok   schedule 10.02.2017