- 浏览: 5086951 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
silence19841230:
先拿走看看
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
masuweng 写道发下源码下载地址吧!三个相关文件打了个包 ...
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
发下源码下载地址吧!
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
水淼火 写道你好,我使用以后,图标不显示,应该怎么引用呢,谢谢 ...
前端框架iviewui使用示例之菜单+多Tab页布局
环境说明:SpringBoot版本2.0.3.RELEASE(不同版本可能有些差异),Gradle版本4.5.1(这个版本不太关键)
WebSocket是微信小程序的基础,是应用广泛、前景很好的新技术,目前大热。这里简要介绍一下使用SpringBoot框架开发WebSocket应用的基础代码。基本原理不讲了,代码原理见函数注释,画个简单的图说明基本流程,如下图所示。
一、引用支撑包
compile('org.springframework.boot:spring-boot-starter-websocket')
二、WebSocket服务端
三、服务端推送测试Controller
使用浏览器打开“/api/ws/sendOne?message=单发消息内容&id=none”群发消息(需要根据实际情况修改id值,这个值见浏览器或IDE控制台输出信息),“/api/ws/sendAll?message=单发消息内容”单发消息。
四、页面端代码
问题补充:
一、如果希望打成WAR包,部署到TOMCAT中。需要使启动类继承:SpringBootServletInitializer,并加入方法:
/**
* 用于支持打包成WAR,部署到tomcat中。
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AssetApplication.class);
}
二、如果打成WAR包后,在TOMCAT中启动时报错:Multiple Endpoints may not be deployed to the same path
注释掉WebSocketServer类上的@Component注解。
发下源码下载地址吧!
三个相关文件打了个包,如果喜欢可以下载。
发下源码下载地址吧!
WebSocket是微信小程序的基础,是应用广泛、前景很好的新技术,目前大热。这里简要介绍一下使用SpringBoot框架开发WebSocket应用的基础代码。基本原理不讲了,代码原理见函数注释,画个简单的图说明基本流程,如下图所示。
一、引用支撑包
compile('org.springframework.boot:spring-boot-starter-websocket')
二、WebSocket服务端
package com.wallimn.iteye.sp.asset.bus.websocket; import java.io.IOException; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicInteger; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; /** * WebSocket服务端示例 * @author wallimn,http://wallimn.iteye.com * */ @ServerEndpoint(value = "/ws/asset") @Component public class WebSocketServer { private static Logger log = LoggerFactory.getLogger(WebSocketServer.class); private static final AtomicInteger OnlineCount = new AtomicInteger(0); // concurrent包的线程安全Set,用来存放每个客户端对应的Session对象。 private static CopyOnWriteArraySet<Session> SessionSet = new CopyOnWriteArraySet<Session>(); /** * 连接建立成功调用的方法 */ @OnOpen public void onOpen(Session session) { SessionSet.add(session); int cnt = OnlineCount.incrementAndGet(); // 在线数加1 log.info("有连接加入,当前连接数为:{}", cnt); SendMessage(session, "连接成功"); } /** * 连接关闭调用的方法 */ @OnClose public void onClose(Session session) { SessionSet.remove(session); int cnt = OnlineCount.decrementAndGet(); log.info("有连接关闭,当前连接数为:{}", cnt); } /** * 收到客户端消息后调用的方法 * * @param message * 客户端发送过来的消息 */ @OnMessage public void onMessage(String message, Session session) { log.info("来自客户端的消息:{}",message); SendMessage(session, "收到消息,消息内容:"+message); } /** * 出现错误 * @param session * @param error */ @OnError public void onError(Session session, Throwable error) { log.error("发生错误:{},Session ID: {}",error.getMessage(),session.getId()); error.printStackTrace(); } /** * 发送消息,实践表明,每次浏览器刷新,session会发生变化。 * @param session * @param message */ public static void SendMessage(Session session, String message) { try { session.getBasicRemote().sendText(String.format("%s (From Server,Session ID=%s)",message,session.getId())); } catch (IOException e) { log.error("发送消息出错:{}", e.getMessage()); e.printStackTrace(); } } /** * 群发消息 * @param message * @throws IOException */ public static void BroadCastInfo(String message) throws IOException { for (Session session : SessionSet) { if(session.isOpen()){ SendMessage(session, message); } } } /** * 指定Session发送消息 * @param sessionId * @param message * @throws IOException */ public static void SendMessage(String sessionId,String message) throws IOException { Session session = null; for (Session s : SessionSet) { if(s.getId().equals(sessionId)){ session = s; break; } } if(session!=null){ SendMessage(session, message); } else{ log.warn("没有找到你指定ID的会话:{}",sessionId); } } }
三、服务端推送测试Controller
使用浏览器打开“/api/ws/sendOne?message=单发消息内容&id=none”群发消息(需要根据实际情况修改id值,这个值见浏览器或IDE控制台输出信息),“/api/ws/sendAll?message=单发消息内容”单发消息。
package com.wallimn.iteye.sp.asset.bus.websocket; import java.io.IOException; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * WebSocket服务器端推送消息示例Controller * * @author wallimn,http://wallimn.iteye.com * */ @RestController @RequestMapping("/api/ws") public class WebSocketController { @RequestMapping(value="/sendAll", method=RequestMethod.GET) /** * 群发消息内容 * @param message * @return */ String sendAllMessage(@RequestParam(required=true) String message){ try { WebSocketServer.BroadCastInfo(message); } catch (IOException e) { e.printStackTrace(); } return "ok"; } @RequestMapping(value="/sendOne", method=RequestMethod.GET) /** * 指定会话ID发消息 * @param message 消息内容 * @param id 连接会话ID * @return */ String sendOneMessage(@RequestParam(required=true) String message,@RequestParam(required=true) String id){ try { WebSocketServer.SendMessage(id,message); } catch (IOException e) { e.printStackTrace(); } return "ok"; } }
四、页面端代码
<!DOCTYPE html> <!-- 功能:WebSocket使用示例 作者:http://wallimn.iteye.com --> <html> <head> <meta charset="UTF-8"> <title>websocket测试</title> <style type="text/css"> h3,h4{ text-align:center; } </style> </head> <body> <h3>WebSocket测试,在<span style="color:red">控制台</span>查看测试信息输出!</h3> <h4>http://wallimn.iteye.com</h4> <h4> [url=/api/ws/sendOne?message=单发消息内容&id=none]单发消息链接[/url] [url=/api/ws/sendAll?message=群发消息内容]群发消息链接[/url] </h4> <script type="text/javascript"> var socket; if (typeof (WebSocket) == "undefined") { console.log("遗憾:您的浏览器不支持WebSocket"); } else { console.log("恭喜:您的浏览器支持WebSocket"); //实现化WebSocket对象 //指定要连接的服务器地址与端口建立连接 //注意ws、wss使用不同的端口。我使用自签名的证书测试, //无法使用wss,浏览器打开WebSocket时报错 //ws对应http、wss对应https。 socket = new WebSocket("ws://localhost:80/ws/asset"); //连接打开事件 socket.onopen = function() { console.log("Socket 已打开"); socket.send("消息发送测试(From Client)"); }; //收到消息事件 socket.onmessage = function(msg) { console.log(msg.data); }; //连接关闭事件 socket.onclose = function() { console.log("Socket已关闭"); }; //发生了错误事件 socket.onerror = function() { alert("Socket发生了错误"); } //窗口关闭时,关闭连接 window.unload=function() { socket.close(); }; } </script> </body> </html>
问题补充:
一、如果希望打成WAR包,部署到TOMCAT中。需要使启动类继承:SpringBootServletInitializer,并加入方法:
/**
* 用于支持打包成WAR,部署到tomcat中。
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AssetApplication.class);
}
二、如果打成WAR包后,在TOMCAT中启动时报错:Multiple Endpoints may not be deployed to the same path
注释掉WebSocketServer类上的@Component注解。
评论
4 楼
silence19841230
2018-10-24
先拿走看看
3 楼
wallimn
2018-06-29
masuweng 写道
发下源码下载地址吧!
三个相关文件打了个包,如果喜欢可以下载。
2 楼
masuweng
2018-06-29
发下源码下载地址吧!
1 楼
masuweng
2018-06-28
发表评论
-
gradle编译错误:Could not find method compile() for arguments
2020-09-19 10:50 18146编译(IDEA+Gradle)一个别人的工程,出现一个 ... -
netty心跳检查之UDP篇
2019-09-15 08:50 2072部分UDP通信场景中,需要客户端定期发送心跳信息,以获取终 ... -
解决tomcat部署两个SpringBoot应用提示InstanceAlreadyExistsException
2019-06-30 11:49 3128两个SpringBoot应用部署在一个Tomcat中,单独 ... -
Eclipse配置MyBatis代码自动化功能
2019-06-29 10:16 16201.安装插件 Eclipse中,Help->Ecli ... -
vue.js中使用qrcode生成二维码
2019-05-20 00:00 7546一、安装包 npm install qrcodejs2 --s ... -
MySQL插入数据报错: Incorrect string value: '\xFD\xDE'
2019-03-31 23:19 1161我MySQL数据库用的uft-8字符集,插入数据一直很正常 ... -
vue自定义组件并双向绑定属性
2019-03-08 22:46 3189做了两个子组件,原理基本一样,一个是使用原生的select ... -
vue-router简单示例
2019-03-05 00:32 1066写个基本完整、稍有借鉴意义的示例,防止自己忘记。 &l ... -
“联通充值系统繁忙”轻松应对
2019-02-06 11:03 3883大过年的,联通充个值一直报“充值系统繁忙”。昨天晚上试了几 ... -
electron.js数据库应用---导航菜单(element-ui+mysql)
2019-02-05 21:33 2272一、环境搭建 略, ... -
electron.js数据库应用---入门(mysql+element-ui)
2019-01-27 23:19 7265我的机器:Windows10,64 ... -
SpringMVC 在controller层中注入成员变量request,是否线程安全
2018-12-17 21:17 2676@RestController public class ... -
VueJS 组件参数名命名与组件属性转化
2018-12-03 00:00 1987转自:https://www.cnblogs.com/meiy ... -
vue-resource拦截器实现token发送及检验自动化
2018-11-16 22:38 3022用了很长时间vue-resource,最近思考$http发 ... -
element-ui试用手记
2018-10-29 20:25 1668element-ui、iviewui都以vue.js为基础 ... -
iviewui中表格控件中render的使用示例
2018-07-07 16:46 9693示例了如何在表格中显示按钮,如何将代码转化为文字。 i ... -
Tomcat错误“Alias name tomcat does not identify a key entry”解决
2018-07-05 21:39 6141申请到了阿里云的证书后,下载、按照说明生成jks格式证书、 ... -
阿里云免费证书“fileauth.txt内容配置错误”解决
2018-07-05 20:43 5167最近研究微信小程序开发,上阿里云申请了个证书,使用文件验证 ... -
springboot2.0跨域配置
2018-07-04 22:11 5217springboot2.0跨域配置: 一、代码 ... -
微信小程序使用code换openid的方法(JAVA、SpringBoot)
2018-07-01 21:52 10274微信小程序序的代码中提示,使用code换取openid,但 ...
相关推荐
通过SpringBoot2.0整合WebSocket实现服务器主动推送消息到前端,并且前端接收到消息后会进行消息声音弹框提醒。
springboot2.0+websocket的集成,实现群发消息+单对单消息推送.
项目基于springboot2.0 项目实现了websocket点对点和广播两种方式。分别演示了http和ws两种不同协议下的请求。前端采用SocketJs和Stomp.js。主要提供了解决思路。需要的可以在此基础上进行延伸。
主要介绍了SpringBoot2.0整合WebSocket代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
SpringBoot2.0集成WebSocket,实现后台向前端推送信息
SpringBoot项目整合WebSocket+netty实现前后端双向通信(同时支持前端webSocket和socket协议哦) SpringBoot项目整合WebSocket+netty实现前后端双向通信(同时支持前端webSocket和socket协议哦) SpringBoot项目整合...
Java开发基于SpringBoot+WebSocket+Redis分布式即时通讯群聊系统。一个基于Spring Boot + WebSocket + Redis,可快速开发的分布式即时通讯群聊系统。适用于直播间聊天、游戏内聊天、客服聊天等临时性群聊场景。 ...
WebSocket为Web应用程序提供了一种在单个TCP连接上进行全双工通信的方式。Spring Boot框架为开发者提供了集成WebSocket的便利性。本文将详细介绍如何在Spring Boot项目中设置和使用WebSocket。 WebSocket通信协议于...
基于springboot2.0+vue的前后端分离系统权限架构,使用Security控制权限,websocket实时聊天,vue编写前端,另包含可执行mysql脚本
基于Springboot+Websocket的简单聊天室 基于Springboot+Websocket的简单聊天室 基于Springboot+Websocket的简单聊天室 基于Springboot+Websocket的简单聊天室 基于Springboot+Websocket的简单聊天室 基于Springboot+...
springboot整合websocket
应读者要求 写个了一个简单地springboot+websocket实现进度条的demo。
SpringBoot整合WebSocket+nacos注册中心实现多服务通信
基于SpringBoot2.0的开发的系统 易读易懂、界面简洁美观。 具备支付系统通用的支付、对账、清算、账户管理、支付订单管理等功能; 目前已接通微信支付渠道,应用微信公众号商城 在此基础上可二次开发,可以用于所有...
SpringBoot+websocket广播式应用,使用idea ,其中包括单播,广播,多播
springboot+netty+websocket+redis 分布式聊天,实现简单的聊天功能
基于Springboot+Vue+WebSocket的校园交友项目基于Springboot+Vue+WebSocket的校园交友项目基于Springboot+Vue+WebSocket的校园交友项目基于Springboot+Vue+WebSocket的校园交友项目基于Springboot+Vue+WebSocket的...
主要介绍了springboot websocket集群(stomp协议)连接时候传递参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
局域网聊天工具基于springboot+netty websocket,可发送图片表情包。