netty-websocket简单的使用方法
最便捷使用netty-websocket方法
1.pom添加依赖
<dependency>
<groupId>org.yeauty</groupId>
<artifactId>netty-websocket-spring-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.41.Final</version>
</dependency>
2.工具类
package com.xck.service; import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.timeout.IdleStateEvent; import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RestController;
import org.yeauty.annotation.*;
import org.yeauty.pojo.ParameterMap;
import org.yeauty.pojo.Session; import java.io.*;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet; @ServerEndpoint(prefix = "netty-websocket" ) //在yml文件配置参数
@RestController
@Scope("prototype")
public class NettyWebSocketUtil { /*// 注入的时候,给类的 service 注入
private static GpsService gpsService;
@Autowired
public void setChatService(GpsService gpsService) {
NettyWebSocketController.gpsService= gpsService;
}
*/ // concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象
private static CopyOnWriteArraySet<Session> webSocketSet = new CopyOnWriteArraySet<Session>();
/**
* 请求连接
* @param session 请求连接的标识
* @param headers
* @param parameterMap 请求参数(nettyWS只能通过get方式传参数 URL?param="1"¶m2="1")
* @throws IOException
*/
@OnOpen
public void onOpen(Session session, HttpHeaders headers,ParameterMap parameterMap) throws IOException {
webSocketSet.add(session);
String Parame=parameterMap.getParameter("Parame");
String Parame2=parameterMap.getParameter("Parame2");
}
/**
* 请求关闭
* @param session 请求连接的标识
* @throws IOException
*/
@OnClose
public void onClose(Session session) throws IOException {
// System.out.println("one connection closed");
webSocketSet.remove(session);
session.flush();
session.close(); }
/**
* 请求出错
* @param session 请求连接的标识
* @throws IOException
*/
@OnError
public void onError(Session session, Throwable throwable) {
webSocketSet.remove(session);
throwable.printStackTrace();
}
/**
* 接收数据
* @param session 请求连接的标识
* @param message 接收的消息
* @throws IOException
*/
@OnMessage
public void OnMessage(Session session, String message) { } /**
* 发送消息(二进制)
* @param session
* @param bytes
*/
@OnBinary
public void OnBinary(Session session, byte[] bytes) {
for (byte b : bytes) {
System.out.println(b);
}
session.sendBinary(bytes);
} /**
* 群发
* @param obj 发送的消息
*/
public static synchronized void sendMessage2(Object obj) {
//获取session
if (webSocketSet!=null) {
Iterator<Session> it = webSocketSet.iterator();
while (it.hasNext()) {
Session session = it.next();
if (session != null && session.isOpen()) {
session.sendText(obj.toString());
}else if(session != null && !session.isOpen()){
it.remove();
}
}
}
} @OnEvent
public void onEvent(Session session, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
switch (idleStateEvent.state()) {
case READER_IDLE:
System.out.println("read idle");
break;
case WRITER_IDLE:
System.out.println("write idle");
break;
case ALL_IDLE:
System.out.println("all idle");
break;
default:
break;
}
}
}
}
3.配置文件(yml 文件)
netty-websocket: #对应@ServerEndpoint(prefix = "netty-websocket" )
host: 0.0.0.0
path: /gps/WebSocket #访问路径
port: 8099 #端口
4.注册 (本地测试需要加入,打包发布注释掉)
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final Logger logger = LoggerFactory.getLogger(WebMvcConfigurer.class); /**
* 功能描述(打war包时需要注释掉,打成war或者传统方式发布到tomcat中, 相当于启动了两次 )
* @author why
* @date 2019/6/10
* @param
* @return org.springframework.web.socket.server.standard.ServerEndpointExporter
* @description 配置ServerEndpointExporter,配置后会自动注册所有“@ServerEndpoint”注解声明的Websocket Endpoint
*/
/* @Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
*/ }
netty-websocket简单的使用方法的更多相关文章
- Netty+WebSocket简单实现网页聊天
基于Netty+WebSocket的网页聊天简单实现 一.pom依赖 <dependency> <groupId>io.netty</groupId> ...
- WebSocket简单介绍
Java后端WebSocket的Tomcat实现 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSoc ...
- HTTP协议系列(3)---包括WebSocket简单介绍
一.HTTPS HTTP是超文本传输协议,那HTTPS是什么尼?要明白HTTPS是什么先要明白HTTP的缺点,想一下我们在使用HTTP的时候会有那些缺点尼? 1.通信使用的明文(不加密),内容 ...
- netty的简单的应用例子
一.简单的聊天室程序 public class ChatClient { public static void main(String[] args) throws InterruptedExcept ...
- websocket简单入门
今天说起及时通信的时候,突然被问到时用推的方式,还是定时接受的方式,由于之前页面都是用传统的ajax处理,可能对ajax的定时获取根深蒂固了,所以一时之间没有相同怎么会出现推的方式呢?当被提及webs ...
- Netty+WebSocket 获取火币交易所数据项目
Netty+WebSocket 获取火币交易所时时数据项目 先附上项目项目GitHub地址 spring-boot-netty-websocket-huobi 项目简介 本项目使用 SpringBoo ...
- Netty学习——基于netty实现简单的客户端聊天小程序
Netty学习——基于netty实现简单的客户端聊天小程序 效果图,聊天程序展示 (TCP编程实现) 后端代码: package com.dawa.netty.chatexample; import ...
- netty websocket
1 全局保存websocket的通道 NettyConfig.java public class NettyConfig { public static ChannelGroup group = n ...
- netty 实现简单的rpc调用
yls 2020/5/23 netty 实现简单rpc准备 使用netty传输java bean对象,可以使用protobuf,也可以通过json转化 客户端要将调用的接口名称,方法名称,参数列表的类 ...
- CocoaPods 的简单快速安装方法
CocoaPods 的简单快速安装方法(Setting up CocoaPods master repo 卡着不动,是因为淘宝镜像已经不能用了. 一.git clone方法安装cocoapods 镜像 ...
随机推荐
- android studio 使用第三方模拟器连接方法
安装完模拟器后,要使用adb命令Android studio才能识别出来: 打开cmd,输入:adb connect 127.0.0.1:26944.如下: 海马玩模拟器的端口号是26944. 逍遥安 ...
- OSPF基本详解
- 【Vue.js游戏机实战】- Vue.js实现老虎-机抽奖总结
大家好!先上图看看本次案例的整体效果. 完整版实战课程附源码:[Vue.js游戏机实战]- Vue.js实现老虎-机抽奖 实现思路: Vue component实现老虎-机组件,可以嵌套到任意要使用的 ...
- fmex挂单挖矿
最近fmex上线挂单挖矿,针对挂单写了个程序,"跟随盘口,避免成交",0成本薅羊毛. 代码在 https://github.com/xiaoxiaoleo/fmexminer 使用 ...
- 单点登录 sso -- cas CAS 原理 流程 分析
Yelu大学研发的CAS(Central Authentication Server) 下面就以耶鲁大学研发的CAS为分析依据,分析其工作原理.首先看一下最上层的项目部署图: 部署项目时需要部署一个独 ...
- 《Linux设备驱动程序》第三版 scull编译 Ubuntu18.04
0 准备工作. 0.0 系统环境:Ubuntu18.04.1 amd64. 0.1 安装必要软件包 1 sudo apt install build-essential bison flex libs ...
- eQTL | Expression quantitative trait loci | 表达数量性状基因座 | QTL | 数量性状位点
到底什么是eQTL? eQTL和QTL之间有什么联系?为什么说QTL比eQTL难很多? QTL和GWAS有什么关系? GTEx数据库里的eQTL数据如何利用? 说eQTL之前必须先解释QTL,QTL, ...
- Win10 设备管理器一个USB设备描述符请求失败解决方法
问题:进入设备管理器,发现[通用串行总线控制器]下有一项带有黄色[!]未知USB设备(设备描述符请求失败). 或者 解决方法如下: 1.点击Windows键 +R或者(点击系统桌面左下角[开始],在开 ...
- itop 环境
iTop,即IT运营门户(IT Operation Portal),是一个开源web应用程序,用于IT环境的日常运营.它基于ITIL最佳实践,而又不拘泥于任何具体流程.它很灵活,可以适应不管是非正 式 ...
- 002 docker基本的命令
可以参看文章: URL: https://www.yiibai.com/docker/docker-useful-commands.html