Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
797 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ | |
/.idea/ | ||
/.settings/ | ||
.classpath | ||
.project | ||
_book | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ | |
/.idea/ | ||
/.settings/ | ||
.classpath | ||
.project | ||
_book | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
samples/sse-real-time-web/src/main/java/com/waylau/rest/filter/CrossDomainFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.waylau.rest.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import javax.ws.rs.container.ContainerResponseFilter; | ||
|
||
/** | ||
* 说明:支持 Cross-domain 请求 | ||
* | ||
* @author <a href="http://www.waylau.com">waylau.com</a> 2015年8月22日 | ||
*/ | ||
public class CrossDomainFilter implements ContainerResponseFilter { | ||
|
||
/** | ||
* | ||
*/ | ||
public CrossDomainFilter() { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see javax.ws.rs.container.ContainerResponseFilter#filter(javax.ws.rs.container.ContainerRequestContext, javax.ws.rs.container.ContainerResponseContext) | ||
*/ | ||
@Override | ||
public void filter(ContainerRequestContext requestContext, | ||
ContainerResponseContext responseContext) throws IOException { | ||
|
||
// 响应头添加了对允许访问的域,* 代表是全部域 | ||
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*"); | ||
|
||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
samples/sse-real-time-web/src/main/java/com/waylau/rest/filter/CrossDomainFilter2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.waylau.rest.filter; | ||
|
||
/** | ||
* 说明:支持 Cross-domain 请求 | ||
* | ||
* @author <a href="http://www.waylau.com">waylau.com</a> 2015年8月22日 | ||
*/ | ||
public class CrossDomainFilter2 { | ||
|
||
/** | ||
* | ||
*/ | ||
public CrossDomainFilter2() { | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
samples/sse-real-time-web/src/main/webapp/scripts/sse_real_time_cors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
/** | ||
* Created by waylau.com on 2015/8/22 | ||
*/ | ||
|
||
//判断浏览器是否支持 EventSource | ||
if (typeof (EventSource) !== "undefined") { | ||
var source = new EventSource("http://192.168.11.125:8080/webapi/see-events"); | ||
|
||
// 当通往服务器的连接被打开 | ||
source.onopen = function(event) { | ||
console.log("连接开启!"); | ||
|
||
}; | ||
|
||
// 当接收到消息。只能是事件名称是 message | ||
source.onmessage = function(event) { | ||
console.log(event.data); | ||
var data = event.data; | ||
var lastEventId = event.lastEventId; | ||
document.getElementById("x").innerHTML += "\n" + 'lastEventId:'+lastEventId+';data:'+data; | ||
}; | ||
|
||
//可以是任意命名的事件名称 | ||
/* | ||
source.addEventListener('message', function(event) { | ||
console.log(event.data); | ||
var data = event.data; | ||
var lastEventId = event.lastEventId; | ||
document.getElementById("x").innerHTML += "\n" + 'lastEventId:'+lastEventId+';data:'+data; | ||
}); | ||
*/ | ||
|
||
// 当错误发生 | ||
source.onerror = function(event) { | ||
console.log("连接错误!"); | ||
|
||
}; | ||
} else { | ||
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events..." | ||
} |
Oops, something went wrong.