Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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 始终关闭spring mvc的服务器发送事件_Javascript_Java_Spring Mvc_Server Sent Events - Fatal编程技术网

Javascript 始终关闭spring mvc的服务器发送事件

Javascript 始终关闭spring mvc的服务器发送事件,javascript,java,spring-mvc,server-sent-events,Javascript,Java,Spring Mvc,Server Sent Events,我已经做了一个sse演示,但它不起作用,我发布了js和java代码,请告诉我出了什么问题,谢谢 这是js: if (window.EventSource) { var source = new EventSource('http://127.0.0.1:8080/pushweb/push'); source.addEventListener('message', function(e) { console.log(e.data, this.readyState);

我已经做了一个sse演示,但它不起作用,我发布了js和java代码,请告诉我出了什么问题,谢谢

这是js:

if (window.EventSource) {
    var source = new EventSource('http://127.0.0.1:8080/pushweb/push');
    source.addEventListener('message', function(e) {
        console.log(e.data, this.readyState);
    },false);

    source.addEventListener('open', function(e) {
        console.log("conncted",this.readyState);
    },false);
    source.addEventListener('error', function(e) {
        if (e.target.readyState == EventSource.CLOSED) {
            console.log("closed");
        } else if(e.target.readyState == EventSource.CONNECTING){
            console.log("connecting");
        }else {
            console.log("error"+this.readyState);
        }
    }, false);
} else {
    console.log("not support SSE");
}
这是springmvc代码:

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class PushController
{
@RequestMapping(value = "/push", produces = "text/event-stream;charset=UTF-8")
@ResponseBody
public String push()
{
    String str =Long.toString(System.currentTimeMillis());
    return "data:"+str + "\n\n\n";
}

@RequestMapping(value = "/message")
@ResponseBody
public String message()
{
    String stre = "hello";
    return stre;
}
}


在服务器端,您需要使用SSEEmiter发送事件。请参阅(博客)。

在服务器端,您需要使用SSEEmiter发送事件。请参阅(blog)。

如果您需要调试帮助,至少提供预期的和有服务的行为,“告诉我出了什么问题”是不够的。还请阅读和。如果您需要调试帮助,至少提供预期的和有服务的行为,“告诉我出了什么问题”是不够的。请同时阅读和阅读。