转自解放号社区:http://bbs.jointforce.com/topic/25545
一.服务层
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();
}
}
}
}
}
}
二.前台界面监听
<!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空文件
<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>
分享到:
相关推荐
.net websocket 消息推送 该程序用vs2013编写的 主要看代码 别的版本都可仿照 适合初学者使用
webSocket 消息推送 eclipse+springMVC+websocket+sockjs(非maven项目),直接部署就能跑起用,jdk1.7,tomcat7.0以上
1、基于Spring Boot 实现的WebSocket实时数据通信Demo 2、结合Netty实现多客户端之间进行网络通信 3、在Web端建立多客户端之间的通信机制
websocket消息推送消息
消息推送:1.浏览器前台 到 socket 2.生产消息端 到 socket 再从socket 推送到前台 3.代码整理之后,可以和webpy框架合用 4.实现前台无刷新,后台多线程监听消息发送
做的一个简单的小案例,websocket实时推送消息,当执行一个操作后可以通知页面刷新
基于node的前端websocket简单实时推送数据基本用法 含有goEasy基础版本
SpringBoot WebSocket消息推送(群发和指定到个人或多人),导入项目可以直接运行。
基于消息队列、spring boot、websocket实现的消息推送模型代码
android利用WebSocket实现消息推送
Django 使用WebSocket实时消息推送和简易聊天室,使用dwebsocket进行实现,包含了简单的消息推送小功能和聊天室功能的实现,是学习WebSockect的一个基础例子
主要介绍了Java中websocket消息推送的实现代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
rabbitmq+websocket(SpringBoot版)实现分布式消息推送 本来想用websocket做一个消息推送 可是分布式环境下不支持session共享因为服务器不同 所以采用 rabbitMQ+webSocket实现分布式消息推送 生产者将消息 发送给 ...
SpringBoot+WebSocket实现消息推送,SpringBoot+WebSocket+SpringSecurity实现聊天室
Spring+Websocket实现消息的推送 Spring+Websocket实现消息的推送 Spring+Websocket实现消息的推送
主要为大家详细介绍了php实现websocket实时消息推送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
如何使用Postman来发送数据到后端Spring Boot应用程序,并使用WebSocket建立实时通信通道,以便后端应用程序可以向Postman推送消息。 学会这个方法,就能缩短你的项目开发周期,以及提高你的开发效率。如果提出您的...
websocket日志实时推送,web网站,利用tail -f 和websocket实时推送到页面
随着互联网技术的快速发展,基于B/S架构的实时通讯和...使用node.js平台和socket.io库技术设计并实现了一种基于websocket协议的服务器实时消息推送平台。运行测试结果显示能快速稳定地实现消息的实时服务器推送。
Django1.9.2 websocket 实时消息推送 服务端主动推送 调用 send(username, title, data, url) username:用户名 title:消息标题 data:消息内容,ulr:消息内容 ulr