介绍

本项目是对springboot官方提供的websocket进行的netty版本封装,api与原版的完全一致,让广大springboot用户更方便的使用netty版本的websocket。netty与tomcat的相比,占用内存更小,效率更高,在特殊环境下,netty的效率是tomcat的20倍,想更轻松的使用netty版本的websocket,那么现在就来使用它吧!~~

使用说明

  1. 添加maven库
 <dependency><groupId>com.simon</groupId><artifactId>spring-boot-starter-websocket-netty</artifactId><version>0.0.1</version></dependency>
  1. 代码示例
    import com.simon.annotation.*;import com.simon.model.Session;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Component;import java.io.IOException;import java.util.concurrent.CopyOnWriteArraySet;@ServerEndpoint(port = 8081,path = "/websocket/{sid}")@Componentpublic class NettyServer {static Log log= LogFactory.getLog(NettyServer.class);//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。private static int onlineCount = 0;//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。private static CopyOnWriteArraySet<NettyServer> webSocketSet = new CopyOnWriteArraySet<NettyServer>();//与某个客户端的连接会话,需要通过它来给客户端发送数据private Session session;//接收sidprivate String sid="";/*** 连接建立成功调用的方法*/@OnOpenpublic void onOpen(Session session,@PathParam("sid") String sid) {this.session = session;webSocketSet.add(this);     //加入set中addOnlineCount();           //在线数加1log.info("有新窗口开始监听:"+sid+",当前在线人数为" + getOnlineCount());this.sid=sid;try {sendMessage("连接成功");} catch (IOException e) {log.error("websocket IO异常");}}/*** 连接关闭调用的方法*/@OnClosepublic void onClose() {webSocketSet.remove(this);  //从set中删除subOnlineCount();           //在线数减1log.info("有一连接关闭!当前在线人数为" + getOnlineCount());}/*** 收到客户端消息后调用的方法** @param message 客户端发送过来的消息*/@OnMessagepublic void onMessage(String message, Session session) throws IOException {log.info("收到来自窗口"+sid+"的信息:"+message);this.sendMessage(message);}/**** @param session* @param error*/@OnErrorpublic void onError(Session session, Throwable error) {log.error("发生错误");error.printStackTrace();}/*** 实现服务器主动推送*/public void sendMessage(String message) throws IOException {this.session.sendText(message);}/*** 群发自定义消息* */public static void sendInfo(String message,String sid) throws IOException {log.info("推送消息到窗口"+sid+",推送内容:"+message);for (NettyServer item : webSocketSet) {try {//这里可以设定只推送给这个sid的,为null则全部推送if(sid==null) {item.sendMessage(message);}else if(item.sid.equals(sid)){item.sendMessage(message);}} catch (IOException e) {continue;}}}public static synchronized int getOnlineCount() {return onlineCount;}public static synchronized void addOnlineCount() {NettyServer.onlineCount++;}public static synchronized void subOnlineCount() {NettyServer.onlineCount--;}

springboot-websocket-netty相关推荐

  1. springboot整合netty

    前面介绍了netty的基本使用以及和websocket的整合,下面就说说如何用springboot整合netty,毕竟我们是要把netty作为一个服务端的框架整合到我们的项目中去的,总不能用main函 ...

  2. SpringBoot +WebSocket实现简单聊天室功能实例

    SpringBoot +WebSocket实现简单聊天室功能实例) 一.代码来源 二.依赖下载 三.数据库准备(sql) 数据库建表并插入sql 四.resources文件配置 application ...

  3. SpringBoot2+Netty+WebSocket(netty实现websocket)

    ##### 一.SpringBoot2+Netty+WebSocket(netty实现websocket,支持URL参数) 原文链接: https://zhengkai.blog.csdn.net/a ...

  4. SpringBoot WebSocket Stomp

    关注 "弋凡"(YiFan)微信公众号吧 记录简单笔记 做你的最爱 websocket 是什么? 是一种网络通信协议,很多高级功能都需要它 为什么要使用websocket? 已经有 ...

  5. springboot websocket

    大家好,我是烤鸭: 今天想分享一下springboot+websocket. 之前接到一个需求,需要在页面监听后台的数据(输入邮箱后,需要用户打开邮箱,页面监听用户是否点击激活邮件).之前的实现方式, ...

  6. Springboot 整合 Netty 实战(附源码)

    作者:pjmike_pj juejin.im/post/5bd584bc518825292865395d 前言 这一篇文章主要介绍如何用Springboot 整合 Netty,由于本人尚处于学习Net ...

  7. Springboot整合Netty,实现Socket通信

    文章目录 *Springboot整合Netty,实现Socket通信* 1.模拟单客户端 2.模拟单服务端 总结 Springboot整合Netty,实现Socket通信 1.模拟单客户端 引入Net ...

  8. SpringBoot使用Netty实现远程调用

    SpringBoot使用Netty实现远程调用 前言 众所周知我们在进行网络连接的时候,建立套接字连接是一个非常消耗性能的事情,特别是在分布式的情况下,用线程池去保持多个客户端连接,是一种非常消耗线程 ...

  9. springboot websocket 传递 头信息 协议头 token 的前后端解决方案

    文章目录 一.前言 二.js websocket 传递token 2.1 基于协议头 三.后台取出websocket协议头的参数 3.1 取出token 3.2 注意大坑 四.结尾 一.前言 关于sp ...

  10. Springboot + WebSocket 实现在线聊天

    一.后端 1.在Springboot项目的pom.xml中添加依赖 <!--websocket协议--> <dependency><groupId>org.spri ...

最新文章

  1. uml具有多种视图_UML建模与架构文档化
  2. Open quote is expected for attribute {1} associated with an element type name.
  3. 监督学习-KNN最邻近分类算法
  4. mysql 5.6 安装 中文_ubuntu 下mysql 5.6安装、删除和配置中文乱码问题
  5. c语言小游戏 贴吧,【图片】C语言小游戏~贪吃蛇【c语言吧】_百度贴吧
  6. 华为ac配置radius认证服务器_华为AAA结合Radius服务器认证学习
  7. android ndk 架构,NDK需要特别注意的armeabi等架构问题
  8. 腾讯会议共享屏幕时,设置演讲者模式仅自己可见
  9. 新闻管理系统数据库设计
  10. 矢量导数——角速度与矢量的叉乘
  11. Unity3D自由摄像头视角旋转,平移,缩放
  12. 采用Windows PowerShell运行conda activate xx出现 C:\Users\xxx\Documents\WindowsPowerShell\profile.ps1报错问题
  13. android stop 服务,当调用stopService方法时服务不会停止
  14. html内部css调节背景图片的大小,css中怎么改变背景图片大小?
  15. halo_回顾:Halo 3与糖尿病抗争
  16. 基于AD9854的DDS信号发生器设计
  17. 机器学习系列之交叉验证、网格搜索
  18. linux安装软件时/usr/lib/python2.7/site-packages/urlgrabber/grabber.py文件异常
  19. c#轻松实现磁性窗口
  20. 2015.04.14,学习,读书笔记-《把你的英语用起来》学习笔记(1)

热门文章

  1. mysql 根据地图 坐标 查询 周边景区、酒店
  2. java预备作业2 计科1501 乔赫
  3. Unity中Oculus分屏相机和普通相机一键切换
  4. SurfaceViewDemo
  5. 进程返回linux系统编程之管道(二):管道读写规则和Pipe Capacity、PIPE_BUF
  6. CentOS服务器iptables配置
  7. WCF自我学习之(一)
  8. 图解Ubuntu中pidgin登陆IRC
  9. ie打开完成,网页显示空白
  10. kafka整理笔记笔记