一.服务层

  

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>

websocket消息推送实现的更多相关文章

  1. node.js Websocket消息推送---GoEasy

    Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送 速度快,代码简单易懂上手快 浏览器兼容性:GoEasy推送 支持websocket ...

  2. C(++) Websocket消息推送---GoEasy

    Goeasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送!个人感觉goeasy推送更稳定,推送 速度快,代码简单易懂上手快 浏览器兼容性:GoEasy推送 支持websocket ...

  3. 【WebSocket】WebSocket消息推送

    准备使用WebSocket实现Java与Vue或者安卓间的实时通信,实现私密聊天.群聊.查询下资料备用. WebSocket客户端 websocket允许通过JavaScript建立与远程服务器的连接 ...

  4. WebSocket消息推送

    WebSocket协议是基于TCP的一种新的网络协议,应用层,是TCP/IP协议的子集. 它实现了浏览器与服务器全双工(full-duplex)通信,客户端和服务器都可以向对方主动发送和接收数据.在J ...

  5. spring boot下WebSocket消息推送(转)

    原文地址:https://www.cnblogs.com/betterboyz/p/8669879.html WebSocket协议 WebSocket是一种在单个TCP连接上进行全双工通讯的协议.W ...

  6. spring+rabbitmq+stomp搭建websocket消息推送(非spring boot方式)

    前言: 两年前做过spring+activemq+stomp的ws推送,那个做起来很简单,但现在公司用的mq中间件是rabbitmq,因此需要通过rabbitmq去做ws通信.仔细搜了搜百度/谷歌,网 ...

  7. spring boot下WebSocket消息推送

    WebSocket协议 WebSocket是一种在单个TCP连接上进行全双工通讯的协议.WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范.WebSo ...

  8. swoole WebSocket 消息推送

    server.php <?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $r ...

  9. WebSocket消息推送(实现进行聊天)和WebSocket简介

    WebSocket简介 WebSocket是HTML5开始提供的一种浏览器与服务器间进行全双工通讯的网络技术.依靠这种技术可以实现客户端和服务器端的长连接,双向实时通信.特点:事件驱动异步使用ws或者 ...

随机推荐

  1. 2018-2019-2 网络对抗技术 20165301 Exp5 MSF基础应用

    2018-2019-2 网络对抗技术 20165301 Exp5 MSF基础应用 实践原理 1.MSF攻击方法 主动攻击:扫描主机漏洞,进行攻击 攻击浏览器 攻击其他客户端 2.MSF的六个模块 查看 ...

  2. vuex 的使用

    用于多组件共享状态,如果不打算开发大型单页应用,使用 Vuex 可能是繁琐冗余的.确实是如此——如果应用够简单,您最好不要使用 Vuex.可使用简单Bus总线的方式来管理共享的数据详见(http:// ...

  3. ajax和jsonp

    ajax和jsonp 前言:ajax和jsonp可以与后台通信,获取数据和信息,但是又不用刷新整个页面,实现页面的局部刷新. 一.ajax 定义:一种发送http请求与后台进行异步通讯的技术. 原理: ...

  4. windows下mysql配置

    windows下mysql配置   忙活了大半天,总算配置好了,本文献给windows下没试用过Mysql的小白,勿喷 http://blog.csdn.net/z1074907546/article ...

  5. gi常用命令

    git提交代码流程 git status 检查当前代码和主支代码不同的状态 git diff 可指定文件查看这个文件修改的内容 git add . 把自己所有修改的代码提交 git commit 提交 ...

  6. java中static,final,private方法的继承多态问题

    父类中的final  private方法不能被继承,父类中的static方法可以被继承,但是不能重写,这三者都属于前期绑定,java中除了这三种以及构造方法之外剩下的都是后期绑定(运行是绑定). 当父 ...

  7. BZOJ4239 : 巴士走读

    考虑按时刻从早到晚模拟,计算出 f[i]:到达i点的最晚出发时间 g[i]:为了赶上第i辆车的最晚出发时间 然后将所有到达n号点的巴士按到达时间排序,查询的时候二分查找即可. 时间复杂度$O(n\lo ...

  8. npm 升级自身

    用cue-cli 生成新项目 提示升级npm 解决方案: npm install -g npm 呵呵呵,简直太简单了   然而今天是2016.11.11

  9. C++中如何访问全局变量和全局函数

    全局变量和全局函数是相对局部变量和局部函数而言的,不在{}或者for, if 等范围内的都是全局变量或者全局函数,最简单的是在同一个文件中去声明. 例如在mian.cpp中 #include < ...

  10. Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题

    A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...