Springboot-webscoket with sockjs
新建springboot maven工程,引入以下包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>
package com.example.demo.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.socket.WebSocketHandler;import org.springframework.web.socket.config.annotation.EnableWebSocket;import org.springframework.web.socket.config.annotation.WebSocketConfigurer;import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;/*** Created by dingshuo on 2017/5/18.*/@Configuration@EnableWebSocketpublic class WebsocketConfig implements WebSocketConfigurer{@Overridepublic void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {webSocketHandlerRegistry.addHandler(myHandler(),"/ws").setAllowedOrigins("*").withSockJS();}@Beanpublic WebSocketHandler myHandler(){return new com.example.demo.config.WebSocketHandler();}}
package com.example.demo.config;import org.springframework.stereotype.Component;import org.springframework.web.socket.CloseStatus;import org.springframework.web.socket.TextMessage;import org.springframework.web.socket.WebSocketMessage;import org.springframework.web.socket.WebSocketSession;import org.springframework.web.socket.handler.TextWebSocketHandler;import java.util.HashMap;import java.util.Map;import java.util.UUID;/*** Created by dingshuo on 2017/5/18.*/@Componentpublic class WebSocketHandler extends TextWebSocketHandler{public static final Map<Object, WebSocketSession> userSocketSessionMap;static {userSocketSessionMap = new HashMap<Object, WebSocketSession>();}@Overridepublic void afterConnectionEstablished(WebSocketSession session) throws Exception {userSocketSessionMap.put(UUID.randomUUID(),session);System.out.println("建立连接完成");}@Overridepublic void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {switch (message.getPayload().toString()){case "1":sendMsg(session,new TextMessage("A"));break;case "2":sendMsg(session,new TextMessage("B"));break;}System.out.println("处理消息");}@Overridepublic void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {System.out.println("处理消息传出错误");}@Overridepublic void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {System.out.println("处理连接关闭");}private void sendMsg(WebSocketSession session,TextMessage message) throws Exception {for (int i=0;i<100;i++){Thread.sleep(1000);session.sendMessage(message);}}}
<!doctype html><html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script><script src="http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"></script><style>.box {width: 300px;float: left;margin: 0 20px 0 20px;}.box div, .box input {border: 1px solid;-moz-border-radius: 4px;border-radius: 4px;width: 100%;padding: 0px;margin: 5px;}.box div {border-color: grey;height: 300px;overflow: auto;}.box input {height: 30px;}h1 {margin-left: 30px;}body {background-color: #F0F0F0;font-family: "Arial";}</style></head><body lang="en"><h1>Index</h1><div id="first" class="box"><div></div><form><input autocomplete="off" value="Type here..."></input></form></div><script>var sockjs_url = 'http://127.0.0.1:8080/ws';var sockjs = new SockJS(sockjs_url);$('#first input').focus();var div = $('#first div');var inp = $('#first input');var form = $('#first form');var print = function(m, p) {p = (p === undefined) ? '' : JSON.stringify(p);div.append($("<code>").text(m + ' ' + p));div.append($("<br>"));div.scrollTop(div.scrollTop()+10000);};sockjs.onopen = function() {print('[*] open', sockjs.protocol);};sockjs.onmessage = function(e) {print('[.] message', e.data);};sockjs.onclose = function() {print('[*] close');};form.submit(function() {print('[ ] sending', inp.val());sockjs.send(inp.val());inp.val('');return false;});</script></body></html>
Springboot-webscoket with sockjs的更多相关文章
- springboot实现服务器端消息推送(websocket + sockjs + stomp)
服务器端推送技术在web开发中比较常用,可能早期很多人的解决方案是采用ajax向服务器轮询消息,这种方式的轮询频率不好控制,所以大大增加了服务器的压力,后来有了下面的方案:当客户端向服务器发送请求时, ...
- springboot+websocket+sockjs进行消息推送【基于STOMP协议】
springboot+websocket+sockjs进行消息推送[基于STOMP协议] WebSocket是在HTML5基础上单个TCP连接上进行全双工通讯的协议,只要浏览器和服务器进行一次握手,就 ...
- 通过springBoot集成搭建webScoket服务器
前言: 最近工作中有一个需求,就是服务端要主动推送消息给客户端,而我们平常的Http请求只能一请求一响应,为此学习了webScokset通讯技术,以下介绍的是java 通过SpringBoot集成we ...
- Springboot + Websocket + Sockjs + Stomp + Vue + Iview 实现java后端日志显示在前端web页面上
话不多说,看代码. 一.pom.xml 引入spring boot websocket依赖 <dependency> <groupId>org.springframework. ...
- webscoket实战之利用httpsession定向推送
webscoket实战之利用httpsession定向推送 开发框架 springboot 场景 在利用websocket主动推送信息给客户端的过程中,经常会遇到一个普遍需求,就是推送的消息要定向推送 ...
- springboot情操陶冶-web配置(四)
承接前文springboot情操陶冶-web配置(三),本文将在DispatcherServlet应用的基础上谈下websocket的使用 websocket websocket的简单了解可见维基百科 ...
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【六】【引入bootstrap前端框架】
https://blog.csdn.net/linzhefeng89/article/details/78752658 基于springboot+bootstrap+mysql+redis搭建一套完整 ...
- springboot+websocket 归纳收集
websocket是h5后的技术,主要实现是一个长连接跟tomcat的comet技术差不多,但websocket是基于web协议的,有更广泛的支持.当然,在处理高并发的情况下,可以结合tomcat的a ...
- springboot websocket 一篇足够了
什么是WebSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 … 为什么要实现握手监控管理 如果说,连接随意创建,不管的话,会存在错误,broken pipe 表面看单纯报 ...
- 全栈开发——动手打造属于自己的直播间(Vue+SpringBoot+Nginx)
前言 大学的学习时光临近尾声,感叹时光匆匆,三年一晃而过.同学们都忙着找工作,我也在这里抛一份简历吧,欢迎各位老板和猎手诚邀.我们进入正题.直播行业是当前火热的行业,谁都想从中分得一杯羹,直播养活了一 ...
随机推荐
- Vue项目中出现Loading chunk {n} failed问题的解决方法
最近有个Vue项目中会偶尔出现Loading chunk {n} failed的报错,报错来自于webpack进行code spilt之后某些bundle文件lazy loading失败.但是这个问题 ...
- python 对位运算
- 【Leetcode 堆、快速选择、Top-K问题 BFPRT】数组中的第K个最大元素(215)
这道题很强大,引出了很多知识点 题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5 ...
- LeafLet 简单使用
Leaflet 使用 最近在Angular项目中,用到了地图,由于种种原因放弃了百度地图api使用,最后选择了leaflet,简单介绍一下. 介绍: Leaflet 是一个为移动设备设计的交互式地图的 ...
- Oracle存储过程小解
Oracle存储过程小解 1.创建语法 create or replace procedure pro_name( paramIn in type, paramOUt out type, paramI ...
- 【JZOJ4771】【NOIP2016提高A组模拟9.9】爬山
题目描述 国家一级爬山运动员h10今天获得了一张有着密密麻麻标记的地图,在好奇心的驱使下,他又踏上了去爬山的路. 对于爬山,h10有一个原则,那就是不走回头路,于是他把地图上的所有边都标记成了有向边. ...
- sqlite数据库文件导入到sqlserver 2016-03-26 21:55 1292人阅读 评论(1) 收藏
最近在公司做项目,需要做两个版本,都是cs的,然后要求是一个单机版,自带数据库,另一个要进行局域网内的连接,所以公司的大牛设计是,局域网版的用sqlserver2008,单机版的则用sqlite.然后 ...
- Uva 10334
UVa 10334 这道题几乎和UVa 495是一样的. #include<iostream> #include<cstdio> #define mod 1000000 usi ...
- OpenCV 新手教程 之环境配置 + 图片匹配 matchTemplate
1.什么是OpenCV OpenCV的全称是:Open Source Computer Vision Library. OpenCV是一个基于(开源)发行的跨平台计算机视觉库,能够执行在Linux.W ...
- oralce函数 count(*|[distinct|all]x)
[功能]统计数据表选中行x列的合计值. [参数] *表示对满足条件的所有行统计,不管其是否重复或有空值(NULL) all表示对所有的值统计,默认为all distinct只对不同的值统计, 如果有参 ...