最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java中websocket消息推送的实现代码
时间:2017-04-03 编辑:简简单单 来源:一聚教程网
一.服务层
代码如下 | 复制代码 |
packagecom.demo.websocket; importjava.io.IOException; importjava.util.Iterator; importjava.util.concurrent.ConcurrentLinkedQueue; importjavax.servlet.ServletContextEvent; importjavax.servlet.ServletContextListener; importorg.springframework.context.annotation.Configuration; importorg.springframework.web.socket.CloseStatus; importorg.springframework.web.socket.TextMessage; importorg.springframework.web.socket.WebSocketSession; importorg.springframework.web.socket.config.annotation.EnableWebSocket; importorg.springframework.web.socket.config.annotation.WebSocketConfigurer; importorg.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; importorg.springframework.web.socket.handler.TextWebSocketHandler; @Configuration @EnableWebSocket publicclasswebsocketListenerimplementsWebSocketConfigurer, ServletContextListener{ privateConcurrentLinkedQueue privateWebSocketHandlerTest handler; @Override publicvoidcontextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override publicvoidcontextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override publicvoidregisterWebSocketHandlers(WebSocketHandlerRegistry registry) { handler =newWebSocketHandlerTest(); registry.addHandler(handler,"/ws/notifymessage.ws"); registry.addHandler(handler,"/ws/sockjs/notifymessage.ws").withSockJS(); newThread(handler).start(); } classWebSocketHandlerTestextendsTextWebSocketHandlerimplementsRunnable{ @Override publicvoidafterConnectionClosed(WebSocketSession session, CloseStatus status)throwsException { sessions.remove(session); } @Override publicvoidafterConnectionEstablished(WebSocketSession session)throwsException { sessions.add(session); } @Override protectedvoidhandleTextMessage(WebSocketSession session, TextMessage message)throwsException { super.handleTextMessage(session, message); } @Override publicvoidrun() { System.out.println("等待推送...."); try{ inti =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("失败...."); } } privatevoidnofity(String message)throwsIOException { Iterator while(iterator.hasNext()) { WebSocketSession session = iterator.next(); synchronized(session){ if(session.isOpen()){ session.sendMessage(newTextMessage(message)); }else{ iterator.remove(); } } } } } } |
二.前台界面监听
代码如下 | 复制代码 |
websocket测试界面 var websocketPath ="localhost:8080/demo-web"; if('WebSocket'in window) { websocket =newWebSocket("ws://"+websocketPath+"/ws/notifymessage.ws"); }elseif('MozWebSocket'in window) { websocket =newMozWebSocket("ws://"+websocketPath+"/ws/notifymessage.ws"); }else{ websocket =newSockJS("ws://"+websocketPath+"/ws/notifymessage.ws"); } websocket.onopen = function (evnt) { }; websocket.onmessage = function (evnt) { console.log(evnt); }; websocket.onerror = function (evnt) { }; websocket.onclose = function (evnt) { } |
注意web.xml中配置DispatcherServlet控制器
spring-servlet.xml空文件
代码如下 | 复制代码 |
|
-
上一个: js实现九九乘法表效果
-
下一个: Java 垃圾回收机制详解及实例代码
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24