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 镜像 ...
随机推荐
- ICEM-轴
https://yunpan.cn/cuy98EwwXvYkb 访问密码 0897
- vue element-UI Form表单验证
摘自官网 https://element.eleme.cn/#/zh-CN/component/form 保证prop的值等于v-model的值,并且初始化值,这样验证才好使. 可以自定义验证 < ...
- 微信JS-SDK分享功能的.Net实现代码
JS-SDK接口是什么? 为了方便开发者实现微信内的网页(基于微信浏览器访问的网页)功能,比如拍照.选图.语音.位置等手机系统的能力,并方便开发者直接使用微信分享.扫一扫等微信特有的能力,微信推出了J ...
- pt-table-checksum解读【转】
pt-table-checksum是目前可以说是最好的查看主从一致性的工具 先来个使用例子,有助快速上手使用 在主库执行: mysql>GRANT SELECT, PROCESS, SUPER, ...
- 磁盘性能指标--IOPS与吞吐量
磁盘性能指标--IOPS---------------------------------------------------------- IOPS (Input/Output Per ...
- 微信小程序的z-index在苹果ios无效
1.在微信开发者工具可以正常显示 2.在安卓真机手机可以正常显示 3.在ios手机真机无法正常显示 原因:父级view的css属性有 position: fixed; ,把它注释掉即可
- 【Java文件】按UTF-8编码读取文本文件(逐行方式),排序,打印到控制台
代码: package findJavaMemberFunction; import java.io.BufferedReader; import java.io.FileInputStream; i ...
- Java性能分析神器-JProfiler详解(转)
前段时间在给公司项目做性能分析,从简单的分析Log(GC log, postgrep log, hibernate statitistic),到通过AOP搜集软件运行数据,再到PET测试,感觉时间花了 ...
- Java基础 hello world基础实例
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- Docs-.NET-C#-指南-语言参考-关键字-值类型:struct
ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型:struct 1.返回顶部 1. struct(C# 参考) 2015/07/20 struct 类型是一种值类型,通常用来封 ...