使用nodejs文件调用SSE

时间:2018-02-15 09:51:31

标签: node.js html5

我在HTML5 SSE中调用nodejs文件我的代码在下面,但它没有运行。

<script type="text/javascript">
var source = new EventSource("http://localhost:3000/");
source.onmessage = function(event) { } 
</script>

任何人都可以帮忙......?

我正在调用的My Node文件位于下方。

var http = require('http');
var url = require('url');
var report = [];
report = {
    "192.168.1.96": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.98": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.97": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.99": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.110": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.111": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.112": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.123": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.102": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "dr": Rndm(0, 1000001), "du": Rndm(0, 1000001), "or": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.103": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "dr": Rndm(0, 1000001), "du": Rndm(0, 1000001), "or": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.104": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "dr": Rndm(0, 1000001), "du": Rndm(0, 1000001), "or": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.105": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.106": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.107": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.118": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.119": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.120": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.121": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.122": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101)},
    "192.168.1.114": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.115": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101)},
    "192.168.1.116": {"s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101)}
};

function Rndm(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache'});
    var q = url.parse(req.url, true).query;
    console.log("request params "+q.ip);
    if (q.ip!=null) {
        res.end("data: " +JSON.stringify(report[q.ip]))+"\n\n";
    } else {
        res.end("data: " +JSON.stringify(report))+"\n\n";
    }

}).listen(3000);

我的问题是我从该代码中得不到任何东西。谢谢你的帮助......

2 个答案:

答案 0 :(得分:1)

  1. 看起来,SSE请求必须保持活力。
  2. 你需要知道什么是SSE请求&amp;其他请求,以保持SSE请求存活。
  3. 我已按如下方式更新您的代码:

    服务器:

    var http = require('http');
    var fs = require('fs');
    var url = require('url');
    
    var connections = [];
    var report = [];
    report = {
      "192.168.1.96": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.98": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.97": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.99": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.110": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.111": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.112": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.123": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tc": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.102": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "dr": Rndm(0, 1000001), "du": Rndm(0, 1000001), "or": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.103": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "dr": Rndm(0, 1000001), "du": Rndm(0, 1000001), "or": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.104": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "dr": Rndm(0, 1000001), "du": Rndm(0, 1000001), "or": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.105": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.106": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.107": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.118": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.119": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.120": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.121": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.122": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "mu": Rndm(0, 101) },
      "192.168.1.114": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.115": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101) },
      "192.168.1.116": { "s": Rndm(0, 2), "cp": Rndm(0, 101), "tr": Rndm(0, 1000001), "mu": Rndm(0, 101) }
    };
    
    function Rndm(min, max) {
      return Math.floor(Math.random() * (max - min)) + min;
    }
    http.createServer(function(req, res) {
    
      console.log(req.url);
      if (req.url === '/') {
        res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' });
        return fs.createReadStream('./views/index.html').pipe(res);
      }
    
    
      if (req.url === '/sse') {
        res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' });
        connections.push(res);
        var q = url.parse(req.url, true).query;
        console.log("request params " + q.ip);
        if (q.ip != null) {
          res.write("data: " + JSON.stringify(report[q.ip])) + "\n\n";
        } else {
          res.write("data: " + JSON.stringify(report)) + "\n\n";
        }
        return;
      }
    
      res.end('Invalid request');
    
    
    }).listen(3000);
    
    function sendData() {
      for (let connection of connections) {
        connection.write("data: " + JSON.stringify(report) + "\n\n");
      }
      setTimeout(sendData, 1000);
    }
    
    sendData();
    

    HTML:

    <script type="text/javascript">
      var source = new EventSource("/sse");
      source.onmessage = function(event) {
        console.log(event);
      }
    </script>
    

    Here,我已将您的代码放在一起&amp;添加了一些额外的代码来模拟服务器发送的事件。我发现这个link对回答这个问题非常有用。

答案 1 :(得分:0)

对于SSE,您可以使用专为SSE构建的sse-nodejs模块。 请找到网址: - https://www.npmjs.com/package/sse-nodejs

您正在使用 var source = new EventSource(&#34; http://localhost:3000/&#34;); source.onmessage = function(event){}

代替你可以使用 var ev = new EventSource(&#39; / time&#39;); ev.addEventListener();

我希望这对你有用。