spring springmvc js websocket 监听
第一步:web.xml中支持异步。所有的filter及servlet
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<async-supported>true</async-supported>
</filter> <filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 注册中央调度器 -->
<servlet>
<servlet-name>springmvc</servlet-name><!-- 随便 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定springmvc配置文件位置及文件名 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value><!-- springmvc的配置文件,classpath代表类路径下 -->
</init-param>
<!-- 写一个>0的数字,越小优先级越高(<=0和没有设置没区别),表明tomcat服务器在启动的时候,就将DispatcherServlet对象给创建了 -->
<!-- 在tomcat启动时,直接创建当前servlet -->
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
第二步。pom.xml依赖
<!-- for support web socket -->
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope> <!-- 注意,scope必须为provided,否则runtime会冲突,如果使用tomcat 8,还需要将TOMCAT_HOME/lib下的javax.websocket-api.jar一并删除 -->
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
第三步:
package com.ldr.websocket; import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
/**
*
* @author Wang&Yang
* @email 867986155@qq.com
* @date 2018-05-02
* @remark esno company
* @version 1.0.0
* @description:
* 这个类表示启用websocket消息处理,以及收发消息的域 config.enableSimpleBroker("/queue", "/topic");这句表示在/queue", "/topic这两个域上可以向客户端发消息; registry.addEndpoint("/endpointChat").withSockJS();客户端在此连接websocket server
*/
@Configuration /*别忘记了要让spring管理咯*/
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue", "/topic");
} @Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
//注册一个名字为"endpointChat" 的endpoint,并指定 SockJS协议。 点对点-用
registry.addEndpoint("/endpointChat").withSockJS();
} }
package com.ldr.websocket; import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.ldr.base.GsonBean; @Controller
public class WebSocketController { public SimpMessagingTemplate template; @Autowired
public WebSocketController(SimpMessagingTemplate template) {
this.template = template;
} @RequestMapping("/noticeDataGenResult")
public void noticeDataGenResult(@RequestParam(value="fn") String fileName,@RequestParam(value="rc") String genResult/*1为成功,0为失败*/) {
Map<String,String> datas=new HashMap<String,String>(5);
datas.put("fileName", fileName);
datas.put("genResult", genResult);
template.convertAndSend("/topic/getResponse", new GsonBean(200, datas));
}
}
第四步:jsp
<!-- websock.js -->
<script type="text/javascript" src="../sys/js/websock/sockjs.min.js"></script>
<script type="text/javascript" src="../sys/js/websock/stomp.min.js"></script> $(function () {
connect();
}); function connect() {
var sock = new SockJS("../endpointChat");
var stomp = Stomp.over(sock);
stomp.connect('guest', 'guest', function(frame) {
stomp.subscribe('/topic/getResponse', function (response) { //订阅/topic/getResponse 目标发送的消息。这个是在控制器的@SendTo中定义的。
var resultObj=$.parseJSON(response.body);
console.dir(resultObj);
});
}); }
spring springmvc js websocket 监听的更多相关文章
- js 实时监听input中值变化
注意:用到了jquery需要引入jquery.min.js. 需求: 1.每个地方需要分别打分,总分为100; 2.第一个打分总分为40; 3.第二个打分总分为60. 注意:需要判断null.&quo ...
- js 事件监听 冒泡事件
js 事件监听 冒泡事件 的取消 [自己写框架时,才有可能用到] <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitiona ...
- js 事件监听 兼容浏览器
js 事件监听 兼容浏览器 ie 用 attachEvent w3c(firefox/chrome) 用 addEventListener 删除事件监听 ie 用 detachEven ...
- js事件监听机制(事件捕获)总结
在前端开发过程中我们经常会遇到给页面元素添加事件的问题,添加事件的js方法也很多,有直接加到页面结构上的,有使用一些js事件监听的方法,由于各个浏览器对事件冒泡事件监听的机制不同,le浏览器只有事件冒 ...
- js动态监听dom变化
原生js 动态监听dom变化,根据不同的类型绑定不同的处理逻辑 // Firefox和Chrome早期版本中带有前缀 var MutationObserver = window.MutationO ...
- js事件监听机制(事件捕获)
在前端开发过程中我们经常会遇到给页面元素添加事件的问题,添加事件的js方法也很多,有直接加到页面结构上的,有使用一些js事件监听的方法,由于各个浏览器对事件冒泡事件监听的机制不同,le浏览器只有事件冒 ...
- Spring Boot实践——事件监听
借鉴:https://blog.csdn.net/Harry_ZH_Wang/article/details/79691994 https://blog.csdn.net/ignorewho/arti ...
- 【spring源码学习】spring的事件发布监听机制源码解析
[一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...
- Vue.js:监听属性
ylbtech-Vue.js:监听属性 1.返回顶部 1. Vue.js 监听属性 本章节,我们将为大家介绍 Vue.js 监听属性 watch,我们可以通过 watch 来响应数据的变化: 实例 & ...
随机推荐
- matplotlib学习记录 七
# 绘制直方图 # 假设你获取了250部电影的时长(列表a中),希望统计出这些电影时长的分布状态(比如时长为100分钟到 # 120分钟电影的数量,出现的频率)等信息,你应该如何呈现这些数据? fro ...
- redis集群监控之Redis-monitor部
为了对以后有可能面临的redis集群监控做准备,这两天在准备这方面的事情,现在将其中的过程记录一下. 首先是“Ronney-Hua”的这篇文章对三中开源监控软件做了对比 文章地址:https://bl ...
- proc_info_list
内核中每种处理器架构抽象为一个proc_info_list结构体,在arch/arm/include/asm/procinfo.h中定义, struct proc_info_list { unsign ...
- LeetCode(147) Insertion Sort List
题目 Sort a linked list using insertion sort. 分析 实现链表的插入排序 注意: 程序入口的特殊输入判断处理! 节点的链接处理,避免出现断链! AC代码 /** ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...
- 关于host,nslookup,dig 的安装
host,nslookup,dig依赖bind包,所以先看一下系统有没有bind包 命令如下:rpm -qa |grep bind 如果没有或者版本太低请升级安装 命令是:yum install bi ...
- 数学基础:HDU2802-F(N)(寻找循环节)
F(N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- poj 1862 2*根号(n1*n2)问题 贪心算法
题意: 有n个数,要把其中2个数进行2*根号(n1*n2)操作,求剩下最小的那个数是多少? 哭诉:看题目根本没看出来要让我做这个操作. 思路: 每次把最大的,次大的拿出来进行操作 用"优先队 ...
- Leetcode 814. 二叉树剪枝
题目链接 https://leetcode-cn.com/problems/binary-tree-pruning/description/ 题目描述 给定二叉树根结点 root ,此外树的每个结点的 ...
- virtual 三种用法
virtual用法一 #include using namespace std;class A{public: virtual void display(){ cout<<& ...