2017-06-13 78 views
0

我已經做了一個sse演示,但它不工作,我張貼的JS和Java代碼,請告訴我什麼是錯的,謝謝。服務器發送事件與春天mvc總是關閉

這是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; 
} 

}

this is the chrome console result

+0

如果你想調試幫助,至少要提供預期的和適當的行爲,「告訴我什麼是錯的」是不夠的。另請閱讀[help/on-topic]和[問]。 – chtz

回答