com.codingapi.tx.framework.utils.SocketUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tx-client Show documentation
Show all versions of tx-client Show documentation
tx-client project for Spring Boot
package com.codingapi.tx.framework.utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;
/**
* Created by lorne on 2017/7/6.
*/
public class SocketUtils {
public static String getJson(Object msg) {
String json;
try {
ByteBuf buf = (ByteBuf) msg;
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
json = new String(bytes);
} finally {
ReferenceCountUtil.release(msg);
}
return json;
}
public static void sendMsg(final ChannelHandlerContext ctx, final String msg) {
ctx.writeAndFlush(Unpooled.buffer().writeBytes(msg.getBytes()));
}
public static void sendMsg(final Channel ctx, final String msg) {
ctx.writeAndFlush(Unpooled.buffer().writeBytes(msg.getBytes()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy