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 镜像 ...
随机推荐
- jenkins之SSH Publishers环境变量
我使用的是docker部署jenkins,使用172.16.1.245作为部署服务器. 1.问题 在SSH Publishers里执行的环境变量,不是ssh server主机设置的环境变量,这样会导致 ...
- redis-migrate-tool
一.简介 redis-migrate-tool是在redis之间迁移数据的一个方便且有用的工具.他会已服务方式不断同步两边的数据.等到合适时间,中断redis读写,对比双方数据,再替换redis地址即 ...
- 升级springboot导致的业务异步回调积压问题定位
1. 起因 A与B云侧模块特性联调的过程中,端侧发现云侧返回有延迟的情况. 7月19日与A模块一起抓包初步判断,B业务有积压的情况. 7月18日已经转侧B业务现网版本,由于使用一套逻辑.故可能存在请求 ...
- Cesium原理篇:Material【转】
https://www.cnblogs.com/fuckgiser/p/6171245.html Shader 首先,在本文开始前,我们先普及一下材质的概念,这里推荐材质,普及材质的内容都是截取自该网 ...
- Flink 之 Data Sink
首先 Sink 的中文释义为: 下沉; 下陷; 沉没; 使下沉; 使沉没; 倒下; 坐下; 所以,对应 Data sink 意思有点把数据存储下来(落库)的意思: Source 数据源 ---- ...
- Phpstudy 无法启动mysql
原因: 两个mysql版本冲突 本地已经有一个mysql服务(3306)默认开启,再装了phpstudy又会自带一个mysqlla服务(3306) phpstudy启动后会启动mysqlla 发现3 ...
- git命令note
日志查看 git log 太乱? git log --pretty=oneline 版本回退 git reset --hard commit_id git reset --hard HEAD^ 上上版 ...
- Django微信小程序后台开发教程
本文链接:https://blog.csdn.net/qq_43467898/article/details/83187698Django微信小程序后台开发教程1 申请小程序,创建hello worl ...
- CopyOnWrite 思想在 Kafka 源码中的运用
CopyOnWrite 思想在 Kafka 源码中的运用 在 Kafka 的内核源码中,有这么一个场景,客户端在向 Kafka 写数据的时候,会把消息先写入客户端本地的内存缓冲,然后在内存缓冲里形成一 ...
- Netty服务器连接池管理设计思路
应用场景: 在RPC框架中,使用Netty作为高性能的网络通信框架时,每一次服务调用,都需要与Netty服务端建立连接的话,很容易导致Netty服务器资源耗尽.所以,想到连接池技术,将与同一个Nett ...