zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package com.codingapi.tm.framework.utils;
Z 2
3 import io.netty.buffer.ByteBuf;
4 import io.netty.buffer.Unpooled;
5 import io.netty.channel.Channel;
6 import io.netty.channel.ChannelHandlerContext;
7 import io.netty.util.ReferenceCountUtil;
8
9 /**
10  * Created by lorne on 2017/7/6.
11  */
12 public class SocketUtils {
13
14     public static String getJson(Object msg) {
15         String json;
16         try {
17             ByteBuf buf = (ByteBuf) msg;
18             byte[] bytes = new byte[buf.readableBytes()];
19             buf.readBytes(bytes);
20             json = new String(bytes);
21         } finally {
22             ReferenceCountUtil.release(msg);
23         }
24         return json;
25
26     }
27
28     public static void sendMsg(ChannelHandlerContext ctx, String msg){
29         ctx.writeAndFlush(Unpooled.buffer().writeBytes(msg.getBytes()));
30     }
31
32
33     public static void sendMsg(Channel ctx,String msg){
34         ctx.writeAndFlush(Unpooled.buffer().writeBytes(msg.getBytes()));
35     }
36 }