一.服务层
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
package com.demo.websocket;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Configuration
@EnableWebSocket
public class websocketListener implements WebSocketConfigurer, ServletContextListener{
private ConcurrentLinkedQueue<WebSocketSession> sessions = new ConcurrentLinkedQueue<WebSocketSession>();
private WebSocketHandlerTest handler;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
handler = new WebSocketHandlerTest();
registry.addHandler(handler, "/ws/notifymessage.ws" );
registry.addHandler(handler, "/ws/sockjs/notifymessage.ws" ).withSockJS();
new Thread(handler).start();
}
class WebSocketHandlerTest extends TextWebSocketHandler implements Runnable{
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session);
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
sessions.add(session);
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
super .handleTextMessage(session, message);
}
@Override
public void run() {
System.out.println( "等待推送...." );
try {
int i = 0 ;
for (;;) {
synchronized ( this ) {
try {
Thread.sleep( 3000 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (i% 10 == 0 ){
nofity( "消息推送测试......" );
System.out.println( "推送消息了...." );
} else {
System.out.println( "本次不推送消息了...." );
}
i++;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println( "失败...." );
}
}
private void nofity(String message) throws IOException {
Iterator<WebSocketSession> iterator = sessions.iterator();
while (iterator.hasNext()) {
WebSocketSession session = iterator.next();
synchronized (session){
if (session.isOpen()){
session.sendMessage( new TextMessage(message));
} else {
iterator.remove();
}
}
}
}
}
}
|
二.前台界面监听
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<title>Insert title here</title>
</head>
<body>
websocket测试界面
</body>
<script type= "text/javascript" >
var websocketPath = "localhost:8080/demo-web" ;
if ( 'WebSocket' in window) {
websocket = new WebSocket( "ws://" +websocketPath+ "/ws/notifymessage.ws" );
} else if ( 'MozWebSocket' in window) {
websocket = new MozWebSocket( "ws://" +websocketPath+ "/ws/notifymessage.ws" );
} else {
websocket = new SockJS( "ws://" +websocketPath+ "/ws/notifymessage.ws" );
}
websocket.onopen = function (evnt) {
};
websocket.onmessage = function (evnt) {
console.log(evnt);
};
websocket.onerror = function (evnt) {
};
websocket.onclose = function (evnt) {
}
</script>
</html>
|
注意web.xml中配置DispatcherServlet控制器
spring-servlet.xml空文件
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<servlet>
<servlet-name>spring</servlet-name>
<servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
|
以上所述是小编给大家介绍的Java中websocket消息推送的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://www.cnblogs.com/tianzhongshan/archive/2017/02/13/6392752.html
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 95
-
2025-05-25 60
-
2025-05-29 24
-
解析smarty 截取字符串函数 truncate的用法介绍
2025-05-29 55 -
2025-05-25 80
热门评论