2016-03-17 8 views
6

मैं एक्सप्रेस-डब्ल्यूएस https://www.npmjs.com/package/express-ws (एपीआई जो एक्सप्रेस और वेबस्केट क्लाइंट के लिए सर्वर बनाने में मदद करता है) का उपयोग कर रहा हूं।एक्सप्रेस-डब्ल्यू समय-समय पर कस्टम ईवेंट की जांच कैसे करें और स्वचालित रूप से कार्रवाई करें

app.ws('/', function(ws, req) { 
    console.log("New connection") 
    if (content.length > 0) { 
    console.log(content) 
    ws.send(content) 
    } 
    ws.on('message', function(msg, flags) { 
    console.log("Received "+ msg); 
    }); 
    ws.on('data', function(msg, flags) { 
    var data = []; // List of Buffer objects 
    res.on("data", function(chunk) { 
     data.push(chunk); // Append Buffer object 
     console.log(data) 
    }) 
    }) 
}); 

अब आप उपरोक्त कोड के साथ देख सकते हैं, जब भी एक कनेक्शन यह बनाई गई है सामग्री की लंबाई की जाँच करता है और ग्राहक के लिए conetent भेजता है और अधिक से अधिक 0.

रूटर कोड के बाद, वेब अनुरोध पर, अद्यतन करता है फ़ाइल। यदि इस फ़ाइल को संशोधित किया गया था, तो कनेक्शन निर्माण के कुछ समय बाद, इस कनेक्शन को इसके बारे में पता नहीं है और इसलिए फ़ंक्शन भेजने को नहीं कहा जाता है। मैंने fs.watch भी कोशिश की लेकिन मैं इसे काम करने में सक्षम नहीं हूं।

router.post('/run_restart', function(req, res, next) { 
    text = '{"to_do": "run_test", "devices":"all", "argv": { "test": "' + req.body.cmd + '", "cycles": "' + req.body.cycles + '", "awake_for": "' + req.body.wt + '" }}' 
    path = process.env['HOME']+'/Desktop/automation/Stressem/StressemWeb/bin/task.txt' 
    fs.writeFile(path, text) 
    res.render('home.jade', { title: 'Stressem' }); 
}); 

fs.watch(file, function (event) { 
    fs.stat(file, function (err, stats) { 
    if(stats.size>80){ 
     console.log("Event: " + event); 
     fs.readFile(file, 'utf8', function (err, data) { 
     if (err) throw err; 
     content = data.toString(); 
     }); 
    } 
    }); 

जब भी फ़ाइल अपडेट की जाती है, तो मुझे क्या चाहिए, ws.send को वेबस्केट कनेक्शन में से किसी एक के लिए बुलाया जा सकता है।

उत्तर

2

के लिए अब मैं मान लिया है इस

var conn_array = []; 
app.ws('/', function(ws, req) { 
    conn_array.push(ws) 
    console.log("New connection") 

    fs.readFile(file, 'utf8', function (err, data) { 
     if (err) throw err; 
     content = data.toString(); 
     if (content.length > 0) { 
      console.log(content.length) 
      conn_array[0].send(content) 
     } 
    }); 


    ws.on('message', function(msg, flags) { 
     console.log("Received "+ msg); 
    }); 

    ws.on('data', function(msg, flags) { 
     var data = []; // List of Buffer objects 
     res.on("data", function(chunk) { 
      data.push(chunk); // Append Buffer object 
      console.log(data) 
     }) 
    }) 
}); 

function readFile(){ 
    console.log("I am here") 
    fs.readFile(file, 'utf8', function (err, data) { 
     if (err) throw err; 
     content = data.toString(); 
     if (content.length > 0 && conn_array.length>0) conn_array[0].send(content); 
    }) 
} 

var interval = setInterval(readFile, 100000); 

की तरह कुछ के साथ इस हल सिर्फ एक ग्राहक

2

यह सरल कोड एक्सप्रेस के साथ अच्छा काम करता है। यदि कुछ देरी आपके लिए कोई समस्या नहीं है, तो आप इसका उपयोग कर सकते हैं।

setInterval(milisecondsToCheck, checkFunction) 
के लिए

अधिक

http://www.w3schools.com/jsref/met_win_setinterval.asp

https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval

यदि आप इसे इस तरह से उपयोग करते हैं, आप इसे किया अपनी नौकरी के बाद समाप्त कर सकते हैं:

var timer = setInterval(milisecondsToCheck, checkFunction); 

यह साफ करने के लिएः

clearInterval(timer); 
+0

धन्यवाद आप इस में देख rtime के लिए है। लेकिन समस्या वही बना है। मैं इस फ़ंक्शन को किसी विशेष कनेक्शन के लिए कैसे कॉल करूं? –

+0

यह जांचें http://stackoverflow.com/questions/9935920/how-to-write-a-node-js-function-that-waits-for-an-event-to-fire-before-returnin – bmavus

4

चूंकि आपका सर्वर फ़ाइल को बदलता है, इसलिए fs.watch का उपयोग करने की आवश्यकता नहीं है क्योंकि आप पहले से ही जानते हैं कि फ़ाइल कब बदलती है। जो कुछ भी करने के लिए छोड़ दिया गया है वह खुले कनेक्शन की सूची में पुनरावृत्त है और उन्हें नई सामग्री भेजता है।

var connections = []; // Keeps track of all connections 
app.ws('/', function(ws, req) { 
    console.log("New connection") 
    connections.push(ws); // Add the new connection to the list 

    if (content.length > 0) { 
    console.log(content) 
    ws.send(content) 
    } 
    ws.on('message', function(msg, flags) { 
    console.log("Received "+ msg); 
    }); 
    ws.on('data', function(msg, flags) { 
    var data = []; // List of Buffer objects 
    res.on("data", function(chunk) { 
     data.push(chunk); // Append Buffer object 
     console.log(data) 
    }) 
    }) 
    // TODO: Make sure you remove closed connections from `connections` 
    // by listening for the ws `close` event. 
}); 

router.post('/run_restart', function(req, res, next) { 
    text = '{"to_do": "run_test", "devices":"all", "argv": { "test": "' + req.body.cmd + '", "cycles": "' + req.body.cycles + '", "awake_for": "' + req.body.wt + '" }}' 
    path = process.env['HOME']+'/Desktop/automation/Stressem/StressemWeb/bin/task.txt' 
    fs.writeFile(path, text) 
    res.render('home.jade', { title: 'Stressem' }); 

    connections.forEach(function(c){ 
    c.send(text); // Send the new text to all open connections 
    } 
}); 

कृपया ध्यान दें: यदि आप कई प्रक्रियाओं या सर्वर है यह काम नहीं करेगा, लेकिन जब से तुम एक डेटाबेस के बजाय स्थानीय फाइल सिस्टम के लिए लिख रहे हैं, मुझे लगता है यह एक आवश्यकता नहीं है।

संबंधित मुद्दे