最便捷使用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"&param2="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简单的使用方法的更多相关文章

  1. Netty+WebSocket简单实现网页聊天

    基于Netty+WebSocket的网页聊天简单实现 一.pom依赖 <dependency>        <groupId>io.netty</groupId> ...

  2. WebSocket简单介绍

    Java后端WebSocket的Tomcat实现 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSoc ...

  3. HTTP协议系列(3)---包括WebSocket简单介绍

    一.HTTPS     HTTP是超文本传输协议,那HTTPS是什么尼?要明白HTTPS是什么先要明白HTTP的缺点,想一下我们在使用HTTP的时候会有那些缺点尼? 1.通信使用的明文(不加密),内容 ...

  4. netty的简单的应用例子

    一.简单的聊天室程序 public class ChatClient { public static void main(String[] args) throws InterruptedExcept ...

  5. websocket简单入门

    今天说起及时通信的时候,突然被问到时用推的方式,还是定时接受的方式,由于之前页面都是用传统的ajax处理,可能对ajax的定时获取根深蒂固了,所以一时之间没有相同怎么会出现推的方式呢?当被提及webs ...

  6. Netty+WebSocket 获取火币交易所数据项目

    Netty+WebSocket 获取火币交易所时时数据项目 先附上项目项目GitHub地址 spring-boot-netty-websocket-huobi 项目简介 本项目使用 SpringBoo ...

  7. Netty学习——基于netty实现简单的客户端聊天小程序

    Netty学习——基于netty实现简单的客户端聊天小程序 效果图,聊天程序展示 (TCP编程实现) 后端代码: package com.dawa.netty.chatexample; import ...

  8. netty websocket

    1 全局保存websocket的通道  NettyConfig.java public class NettyConfig { public static ChannelGroup group = n ...

  9. netty 实现简单的rpc调用

    yls 2020/5/23 netty 实现简单rpc准备 使用netty传输java bean对象,可以使用protobuf,也可以通过json转化 客户端要将调用的接口名称,方法名称,参数列表的类 ...

  10. CocoaPods 的简单快速安装方法

    CocoaPods 的简单快速安装方法(Setting up CocoaPods master repo 卡着不动,是因为淘宝镜像已经不能用了. 一.git clone方法安装cocoapods 镜像 ...

随机推荐

  1. R绘制韦恩图 | Venn图

    解决方案有好几种: 网页版,无脑绘图,就是麻烦,没有写代码方便 极简版,gplots::venn 文艺版,venneuler,不好安装rJava,参见Y叔 酷炫版,VennDiagram 特别注意: ...

  2. Windows和Linux下putenv()函数导致composer更新失败

    bug复现: 原因: putenv() 函数设置特定的环境变量有可能是一个潜在的安全漏洞,所以这个函数在php配置文件中是默认禁止的,在 php.ini 中查找此函数,然后将此函数删除掉,重载配置即可 ...

  3. 新零售下的 AI智能货柜

    公司有个智能货柜,通过微信扫码开门,拿货,自动扣款,挺智能的.还不错.研究一下原理,网上查了一下. 文章简介: 目前新零售风刮的蛮大,笔者进入该领域近一年,负责过无人便利店.智能货柜.智慧商超等产品, ...

  4. 当fixed元素相互嵌套时,父元素会影响子元素的层叠关系,最好不要嵌套使用fixed

    问题:fixed元素被另一个fixed元素包含的时候在chrome下fixed子元素的定位会受到父元素的影响. 解释:层叠关系是受层叠上下文影响的.文档中的层叠上下文由满足以下任意一个条件的元素形成: ...

  5. Flutter -------- dio网络请求

    dio是Flutter中文网开源的一个强大的Dart Http请求库,支持Restful API.FormData.拦截器.请求取消.Cookie管理.文件上传/下载.超时等... 1.添加依赖# d ...

  6. AndoridSQLite数据库开发基础教程(10)

    AndoridSQLite数据库开发基础教程(10) 添加触发器 触发器(TRIGGER)是由事件来触发某个操作.这些事件包括INSERT.DELETE.UPDATE和UPDATE OF.当数据库系统 ...

  7. 【转载】 深度学习之卷积神经网络(CNN)详解与代码实现(一)

    原文地址: https://www.cnblogs.com/further-further-further/p/10430073.html ------------------------------ ...

  8. 零基础学Python-第二章 :Python基础语法-04.Python程序的书写规则

    #号后面的都是注释 import是导入一个模块 结束

  9. 转 How to Resolve ORA-16009: remote archive log destination must be a STANDBY

    ###sample A primary B STANDBY C STANDBY   问题A 库一直报错 ORA-16009: remote archive log destination must b ...

  10. Shenzhen Wanze Technology Co., Ltd.隐私协议

    本隐私权政策详细说明了Shenzhen Wanze Technology Co., Ltd.团队(“我们”或“我们的”)通过我们的应用程序和网站收集的信息,以及我们将如何使用这些信息. 1.我们不会通 ...