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 来响应数据的变化: 实例 & ...
随机推荐
- for_each_node(node)
遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...
- luogu2123 皇后游戏
好题. 网上看到的范围是:\(T \leq 10\),$ n \leq 50000$, $ a_i,b_i \leq 10^9$. 我们按照贪心惯常的思路考虑交换相邻的两个人.容易发现,对于相邻的两个 ...
- Selenium WebDriver-操作单选框
先判断按钮是否已经被选中 如果没有被选中,才可以点击 #encoding=utf-8 import unittest import time import chardet from selenium ...
- python - 字符串的格式化输出
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_2_str.py@ide: PyCharm Community ...
- jmeter所有版本下载路径
https://archive.apache.org/dist/jmeter/binaries/
- ef添加数据时出错 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException”类型的异常
找半天才找到 ef添加数据时出错原因:数据库表中没有主键 ,就算表中有自增列 Added方法也会报错: - this._db.SaveChanges() “this._db.SaveCh ...
- Game on Tree
D - Game on Tree Time limit : 2sec / Memory limit : 256MB Score : 1100 points Problem Statement Ther ...
- [错误处理]: How to deal with chrome failing to launch GPU process
https://github.com/jupyter/notebook/issues/2836 "export BROWSER=google-chrome" command wor ...
- javascript学习笔记 - 面向对象 理解对象
ECMAScript 中有两种属性:数据属性和访问器属性 一 属性类型 1.数据属性.数据属性有4个描述其行为的特性 [[Configurable]]表示能否通过delete删除属性从而重新定义属性: ...
- 九度oj 题目1443:Tr A
题目描述: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. 输入: 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n & ...