Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 服务器已发送带有node.js(express)的事件/事件源_Javascript_Json_Node.js_Express_Server Sent Events - Fatal编程技术网

Javascript 服务器已发送带有node.js(express)的事件/事件源

Javascript 服务器已发送带有node.js(express)的事件/事件源,javascript,json,node.js,express,server-sent-events,Javascript,Json,Node.js,Express,Server Sent Events,我试图用SSE将JSON数据发送到浏览器,但我似乎无法正确获取,我不知道为什么 服务器端如下所示: var express = require("express"), app = express(), bodyParser = require('body-parser'); app.use(express.static(__dirname + '/')); app.use(bodyParser.urlencoded({ extended: true }

我试图用SSE将JSON数据发送到浏览器,但我似乎无法正确获取,我不知道为什么

服务器端如下所示:

var express     = require("express"),
    app         = express(),
    bodyParser  = require('body-parser');

app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var testdata = "This is my message";

app.get('/connect', function(req, res){
    res.writeHead(200, {
      'Connection': 'keep-alive',
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache'
    });

    setInterval(function(){
      console.log('writing ' + testdata);
      res.write('data: {"msg": '+ testdata +'}\n\n');
    }, 1000);
});

/*
app.post('/message', function(req, res) {
  testdata = req.body;
});
*/

var port = 8080;
app.listen(port, function() {
  console.log("Running at Port " + port);
});
res.write('data: ' + testdata + '\n\n');
<script>
    var source = new EventSource('/connect');
    source.onmessage = function(e) {
        var jsonData = JSON.parse(e.data);
        alert("My message: " + jsonData.msg);
    };
</script>
如您所见,我已经注释掉了post内容,但最终我希望将testdata用作JSON本身,如下所示:

var express     = require("express"),
    app         = express(),
    bodyParser  = require('body-parser');

app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var testdata = "This is my message";

app.get('/connect', function(req, res){
    res.writeHead(200, {
      'Connection': 'keep-alive',
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache'
    });

    setInterval(function(){
      console.log('writing ' + testdata);
      res.write('data: {"msg": '+ testdata +'}\n\n');
    }, 1000);
});

/*
app.post('/message', function(req, res) {
  testdata = req.body;
});
*/

var port = 8080;
app.listen(port, function() {
  console.log("Running at Port " + port);
});
res.write('data: ' + testdata + '\n\n');
<script>
    var source = new EventSource('/connect');
    source.onmessage = function(e) {
        var jsonData = JSON.parse(e.data);
        alert("My message: " + jsonData.msg);
    };
</script>
客户端如下所示:

var express     = require("express"),
    app         = express(),
    bodyParser  = require('body-parser');

app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var testdata = "This is my message";

app.get('/connect', function(req, res){
    res.writeHead(200, {
      'Connection': 'keep-alive',
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache'
    });

    setInterval(function(){
      console.log('writing ' + testdata);
      res.write('data: {"msg": '+ testdata +'}\n\n');
    }, 1000);
});

/*
app.post('/message', function(req, res) {
  testdata = req.body;
});
*/

var port = 8080;
app.listen(port, function() {
  console.log("Running at Port " + port);
});
res.write('data: ' + testdata + '\n\n');
<script>
    var source = new EventSource('/connect');
    source.onmessage = function(e) {
        var jsonData = JSON.parse(e.data);
        alert("My message: " + jsonData.msg);
    };
</script>

var source=neweventsource('/connect');
source.onmessage=函数(e){
var jsonData=JSON.parse(e.data);
警报(“我的消息:+jsonData.msg”);
};
我看到控制台日志,但没有看到警报。

尝试发送正确的JSON(
testdata
未在输出中引用):

但最好是:

res.write('data: ' + JSON.stringify({ msg : testdata }) + '\n\n');

我曾在某个阶段引用过它,但当时我可能还有其他失败:)谢谢!我现在正在完成我的消息和更大的JSON。在我的另一个问题上完成了工作,请参阅我的另一个问题的完整代码:我只是在寻找这样一个示例,非常有用!