package com.ruiguang.controller;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruiguang.config.NettyClientBootstrap;
import com.ruiguang.job.test.nettysocket.nettyclient.nettyclient.NettyClient;
import com.ruiguang.job.test.nettysocket.nettyclient.test.ChannelMap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import lombok.extern.java.Log;
@Log
@Controller
@RequestMapping("page")
public class PageController {
NettyClientBootstrap bootstrap;
@RequestMapping("/info")
public String getIndex(){
System.out.println("---1---");
return "Index";
}
@ResponseBody
@RequestMapping("/online")
public String getsocketon(String str){
System.out.println("----init---");
ConcurrentHashMap<String, Channel> channelHashMap = ChannelMap.getChannelHashMap();
Channel channel = channelHashMap.get(str);
// 判断是否活跃
if(channel==null || !channel.isActive()){
ChannelMap.getChannelHashMap().remove(str);
return "连接已经中断";
}
String receiveStr="48 47 12 00 17 01 16 09 03 11 34 20 02 30 30 30 30 30 30 30 30 30 30 00 f2";
byte[] bytes = hexStringToByteArray(receiveStr);
System.out.println("---receiveStr---"+receiveStr);
// 指令发送
//ByteBuf bufff = Unpooled.buffer();
ByteBuf bufx = Unpooled.copiedBuffer(bytes);
// 根据具体业务传输数据
//bufff.writeBytes(receiveStr);
channel.writeAndFlush(bufx).addListener((ChannelFutureListener) future -> {
StringBuilder sb = new StringBuilder();
if(!StringUtils.isEmpty(str)){
sb.append("【").append(str).append("】");
}
if (future.isSuccess()) {
System.out.println(sb.toString()+"回写成功"+receiveStr);
} else {
System.out.println(sb.toString()+"回写失败"+receiveStr);
}
});
return "ok";
}
@ResponseBody
@RequestMapping("/scoket")
public void getsocket(){
System.out.println("----init---");
/*if (bootstrap == null) {
System.out.println("----建立连接---");
try {
//连接 输入服务器的端口和ip
bootstrap = new NettyClientBootstrap(5100, "localhost");
} catch (InterruptedException e) {
e.printStackTrace();
log.info(">>>>>>>>> server socket 连接失败");
}
}*/
System.out.println("----发送消息---");
String str="48 47 12 00 87 00 16 09 02 11 10 26 21 30 30 30 30 30 30 30 30 30 30 60 57 08 B5 0D 71 8E 74 94 5E 30 29 15 D1 9A D4 AA C2 FD 1E 53 E9 3A E1 CA F5 CF A2 6A 6A 74 2B 8F 87 B6 DF A1 5B CA 05 03 1F 3F 86 CF 8A 0C 05 85 7D 94 65 DE 12 4A A5 E3 EF 46 CD DE C8 13 5F 4C 17 2C 08 41 8F 31 99 44 55 AA B5 A5 A8 25 2A 8D 97 FC 22 76 11 50 02 67 05 30 D0 1D 5B 51 5A A1 11";
byte[] bytes = hexStringToByteArray(str);
if(bootstrap==null){
System.out.println("----发送消息 null---");
}else{
System.out.println("----发送消息 xxx---");
ChannelFuture writeAndFlush = bootstrap.socketChannel.writeAndFlush(bytes);
}
}
public byte[] hexStringToByteArray(String hexString) {
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
byte[] bytes = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
.digit(hexString.charAt(i + 1), 16));
}
return bytes;
}
}
Netty 实现scoket 主动推送数据到服务和服务端实现方式
需积分: 30 90 浏览量
2022-09-03
18:33:24
上传
评论 1
收藏 8KB ZIP 举报
ruiguang21
- 粉丝: 5283
- 资源: 34
最新资源
- apk.tw_LineLite_v8a_v.2.17.1_sign.apk
- Elasticsearch实战:构建高效搜索系统的秘诀.zip
- HTML+CSS+JS网页设计:从入门到精通.zip
- 数据库课程设计:从理论到实践的全面指南.zip
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0