ฉันกำลังพยายามเรนเดอร์เทมเพลต pug ของฉันอีกครั้งผ่าน setInterval() เป็นไปได้ไหม?

ฉันกำลังพยายามเรนเดอร์เทมเพลต 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)
            });

person NomNomNom    schedule 09.02.2017    source แหล่งที่มา
comment
คุณไม่จำเป็นต้องอัปเดต index.pug แต่ควรอัปเดตเพจด้วยข้อมูลที่ส่งไปยังไคลเอนต์ผ่าน socket.io   -  person mk12ok    schedule 10.02.2017
comment
การอัปเดตเพจหมายความว่าฉันอัปเดต index.pug หรือไม่ ฉันไม่ค่อยเข้าใจว่าจะทำอย่างไรกับข้อมูลที่เข้ามาในเพจผ่าน socket.on() console.log(data) จะพิมพ์ออกมาได้ดี แต่ฉันไม่รู้ว่าจะส่งข้อมูลนั้นไปยังตัวแปร pug ได้อย่างไร . พี= ข้อมูล   -  person NomNomNom    schedule 10.02.2017
comment
ฉันหมายความว่าคุณสามารถอัปเดตเพจด้วยการจัดการ DOM ด้วยจาวาสคริปต์ธรรมดาหรือ jquery เท่าที่ฉันเข้าใจ คุณจะส่งอาร์เรย์พร้อมผลลัพธ์ของการสืบค้น mysql คุณสามารถแสดงข้อมูลบนเพจได้อย่างง่ายดายหลายวิธี โปรดแสดงโค้ด html ของเพจของคุณ แล้วเราจะดำเนินการแก้ไขให้   -  person mk12ok    schedule 10.02.2017


คำตอบ (1)


แทนที่จะเรนเดอร์ไฟล์ pug ใหม่ คุณสามารถลองทำสิ่งนี้:

เซิร์ฟเวอร์ของคุณ (รหัสเดียวกันมาก):

const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const pug = require('pug');

app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.get('/', function(req, res) {
    res.render('index.pug');
});

setInterval(function() {
    io.emit('data', 'random number: ' + Math.random().toString());
}, 1000);

io.on('connection', function (socket) {
    console.log('client connected');       
    socket.on('disconnect', function() {
        console.log('client disconnected');
    });  
});

http.listen(3001, function(){
  console.log('listening on *:3001');
});

และตัวอย่างของ index.pug:

doctype html
html
  head
    title Testing socket.io
  body
    h1 Testing socket.io
    br
    h3(id="status") not connected 
    br
    p(id="data") 
    script(src="/socket.io/socket.io.js")
    script.
      var socket = io();

      socket.on('connect', function() {
        document.getElementById("status").innerHTML = "connected";   
      });

      socket.on('data', function(data) {
        document.getElementById("data").innerHTML = "Received " + data;
      });
person mk12ok    schedule 10.02.2017