[Update]增加多屏互动Netty通信library

This commit is contained in:
chenfufeng
2022-02-09 15:34:13 +08:00
parent 1c5b2761bf
commit 2f5c5f9b28
27 changed files with 2114 additions and 15 deletions

View File

@@ -0,0 +1,48 @@
package com.mogo.telematic;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class NetworkUtils {
public static String getIPAddress(Context context) {
NetworkInfo info = ((ConnectivityManager)
context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return "当前网络ip是" + inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
if (ipAddress == 0) return "未连接wifi";
return "当前网络ip是" + ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
+ (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
}
} else {
//当前无网络连接,请在设置中打开网络
return "当前无网络连接,请在设置中打开网络";
}
return "IP获取失败";
}
}

View File

@@ -0,0 +1,433 @@
package com.mogo.telematic.client;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import com.mogo.telematic.client.handler.NettyClientHandler;
import com.mogo.telematic.client.listener.MessageStateListener;
import com.mogo.telematic.client.listener.NettyClientListener;
import com.mogo.telematic.client.status.ConnectState;
import java.util.concurrent.TimeUnit;
import io.netty.bootstrap.Bootstrap;
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 io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
/**
* TCP 客户端
*/
public class NettyTcpClient {
private static final String TAG = "NettyTcpClient";
private EventLoopGroup group;
private NettyClientListener listener;
private Channel channel;
private boolean isConnect = false;
/**
* 最大重连次数
*/
private int MAX_CONNECT_TIMES = Integer.MAX_VALUE;
private int reconnectNum = MAX_CONNECT_TIMES;
private boolean isNeedReconnect = true;
private boolean isConnecting = false;
private long reconnectIntervalTime = 5000;
private static final Integer CONNECT_TIMEOUT_MILLIS = 5000;
private String host;
private int tcp_port;
private int mIndex;
/**
* 心跳间隔时间
*/
private long heartBeatInterval = 5;//单位秒
/**
* 是否发送心跳
*/
private boolean isSendheartBeat = false;
/**
* 心跳数据可以是String类型也可以是byte[].
*/
private Object heartBeatData;
private String packetSeparator;
private int maxPacketLong = 1024;
private void setPacketSeparator(String separator) {
this.packetSeparator = separator;
}
private void setMaxPacketLong(int maxPacketLong) {
this.maxPacketLong = maxPacketLong;
}
private NettyTcpClient(String host, int tcp_port, int index) {
this.host = host;
this.tcp_port = tcp_port;
this.mIndex = index;
}
public int getMaxConnectTimes() {
return MAX_CONNECT_TIMES;
}
public long getReconnectIntervalTime() {
return reconnectIntervalTime;
}
public String getHost() {
return host;
}
public int getTcp_port() {
return tcp_port;
}
public int getIndex() {
return mIndex;
}
public long getHeartBeatInterval() {
return heartBeatInterval;
}
public boolean isSendheartBeat() {
return isSendheartBeat;
}
public void connect() {
if (isConnecting) {
return;
}
Thread clientThread = new Thread("client-Netty") {
@Override
public void run() {
super.run();
isNeedReconnect = true;
reconnectNum = MAX_CONNECT_TIMES;
connectServer();
}
};
clientThread.start();
}
private void connectServer() {
synchronized (NettyTcpClient.this) {
ChannelFuture channelFuture = null;
if (!isConnect) {
isConnecting = true;
group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap().group(group)
.option(ChannelOption.TCP_NODELAY, true)//屏蔽Nagle算法试图
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (isSendheartBeat) {
ch.pipeline().addLast("ping", new IdleStateHandler(0, heartBeatInterval, 0, TimeUnit.SECONDS));//5s未发送数据回调userEventTriggered
}
//黏包处理,需要客户端、服务端配合
if (!TextUtils.isEmpty(packetSeparator)) {
ByteBuf delimiter= Unpooled.buffer();
delimiter.writeBytes(packetSeparator.getBytes());
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(maxPacketLong,delimiter));
} else {
ch.pipeline().addLast(new LineBasedFrameDecoder(maxPacketLong));
}
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new NettyClientHandler(listener, mIndex, isSendheartBeat, heartBeatData,packetSeparator));
}
});
try {
channelFuture = bootstrap.connect(host, tcp_port).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (channelFuture.isSuccess()) {
Log.e(TAG, "连接成功");
reconnectNum = MAX_CONNECT_TIMES;
isConnect = true;
channel = channelFuture.channel();
} else {
Log.e(TAG, "连接失败");
isConnect = false;
}
isConnecting = false;
}
}).sync();
// Wait until the connection is closed.
channelFuture.channel().closeFuture().sync();
Log.e(TAG, " 断开连接");
} catch (Exception e) {
e.printStackTrace();
} finally {
isConnect = false;
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_CLOSED, mIndex);
if (null != channelFuture) {
if (channelFuture.channel() != null && channelFuture.channel().isOpen()) {
channelFuture.channel().close();
}
}
group.shutdownGracefully();
reconnect();
}
}
}
}
public void disconnect() {
Log.e(TAG, "disconnect");
isNeedReconnect = false;
group.shutdownGracefully();
}
public void reconnect() {
Log.e(TAG, "reconnect");
if (isNeedReconnect && reconnectNum > 0 && !isConnect) {
reconnectNum--;
SystemClock.sleep(reconnectIntervalTime);
if (isNeedReconnect && reconnectNum > 0 && !isConnect) {
Log.e(TAG, "重新连接");
connectServer();
}
}
}
/**
* 异步发送
*
* @param data 要发送的数据
* @param listener 发送结果回调
* @return 方法执行结果
*/
public boolean sendMsgToServer(String data, final MessageStateListener listener) {
boolean flag = channel != null && isConnect;
if (flag) {
String separator = TextUtils.isEmpty(packetSeparator) ? System.getProperty("line.separator") : packetSeparator;
ChannelFuture channelFuture = channel.writeAndFlush(data + separator).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
listener.isSendSuccss(channelFuture.isSuccess());
}
});
}
return flag;
// ByteBuf buffer = Unpooled.copiedBuffer(data, Charset.forName("UTF-8"));
// if (flag) {
// channel.writeAndFlush(buffer).addListener(listener);
// }
// return flag;
// byte[] bytes = strToByteArray(data);
//
// return sendMsgToServer(bytes,listener);
}
/**
* 同步发送
*
* @param data 要发送的数据
* @return 方法执行结果
*/
public boolean sendMsgToServer(String data) {
boolean flag = channel != null && isConnect;
if (flag) {
String separator = TextUtils.isEmpty(packetSeparator) ? System.getProperty("line.separator") : packetSeparator;
ChannelFuture channelFuture = channel.writeAndFlush(data + separator).awaitUninterruptibly();
return channelFuture.isSuccess();
}
return false;
}
public boolean sendMsgToServer(byte[] data, final MessageStateListener listener) {
boolean flag = channel != null && isConnect;
if (flag) {
ByteBuf buf = Unpooled.copiedBuffer(data);
channel.writeAndFlush(buf).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
listener.isSendSuccss(channelFuture.isSuccess());
}
});
}
return flag;
}
/**
* 获取TCP连接状态
*
* @return 获取TCP连接状态
*/
public boolean getConnectStatus() {
return isConnect;
}
public boolean isConnecting() {
return isConnecting;
}
public void setConnectStatus(boolean status) {
this.isConnect = status;
}
public void setListener(NettyClientListener listener) {
this.listener = listener;
}
public byte[] strToByteArray(String str) {
if (str == null) {
return null;
}
byte[] byteArray = str.getBytes();
return byteArray;
}
/**
* 构建者创建NettyTcpClient
*/
public static class Builder {
/**
* 最大重连次数
*/
private int MAX_CONNECT_TIMES = Integer.MAX_VALUE;
/**
* 重连间隔
*/
private long reconnectIntervalTime = 5000;
/**
* 服务器地址
*/
private String host;
/**
* 服务器端口
*/
private int tcp_port;
/**
* 客户端标识,(因为可能存在多个连接)
*/
private int mIndex;
/**
* 是否发送心跳
*/
private boolean isSendheartBeat;
/**
* 心跳时间间隔
*/
private long heartBeatInterval = 5;
/**
* 心跳数据可以是String类型也可以是byte[].
*/
private Object heartBeatData;
private String packetSeparator;
private int maxPacketLong = 1024;
public Builder() {
this.maxPacketLong = 1024;
}
public Builder setPacketSeparator(String packetSeparator) {
this.packetSeparator = packetSeparator;
return this;
}
public Builder setMaxPacketLong(int maxPacketLong) {
this.maxPacketLong = maxPacketLong;
return this;
}
public Builder setMaxReconnectTimes(int reConnectTimes) {
this.MAX_CONNECT_TIMES = reConnectTimes;
return this;
}
public Builder setReconnectIntervalTime(long reconnectIntervalTime) {
this.reconnectIntervalTime = reconnectIntervalTime;
return this;
}
public Builder setHost(String host) {
this.host = host;
return this;
}
public Builder setTcpPort(int tcp_port) {
this.tcp_port = tcp_port;
return this;
}
public Builder setIndex(int mIndex) {
this.mIndex = mIndex;
return this;
}
public Builder setHeartBeatInterval(long intervalTime) {
this.heartBeatInterval = intervalTime;
return this;
}
public Builder setSendheartBeat(boolean isSendheartBeat) {
this.isSendheartBeat = isSendheartBeat;
return this;
}
public Builder setHeartBeatData(Object heartBeatData) {
this.heartBeatData = heartBeatData;
return this;
}
public NettyTcpClient build() {
NettyTcpClient nettyTcpClient = new NettyTcpClient(host, tcp_port, mIndex);
nettyTcpClient.MAX_CONNECT_TIMES = this.MAX_CONNECT_TIMES;
nettyTcpClient.reconnectIntervalTime = this.reconnectIntervalTime;
nettyTcpClient.heartBeatInterval = this.heartBeatInterval;
nettyTcpClient.isSendheartBeat = this.isSendheartBeat;
nettyTcpClient.heartBeatData = this.heartBeatData;
nettyTcpClient.packetSeparator = this.packetSeparator;
nettyTcpClient.maxPacketLong = this.maxPacketLong;
return nettyTcpClient;
}
}
}

View File

@@ -0,0 +1,197 @@
package com.mogo.telematic.client;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.net.InetAddress;
import java.util.ArrayList;
public class NsdClient {
public static final String TAG = "NsdClient";
/**
* NSD_SERVICE_NAME和NSD_SERVER_TYPE需要与服务器端完全一致
*/
private final String NSD_SERVER_TYPE = "_http._tcp.";
private NsdManager.DiscoveryListener mDiscoveryListener;
private NsdManager.ResolveListener mResolverListener;
private NsdManager mNsdManager;
private Context mContext;
private String mServiceName;
private IServerFound mIServerFound;
/**
* 用来存储解析后的网络对象列表,包含完整数据
*/
private ArrayList<NsdServiceInfo> mNsdServiceInfoList = new ArrayList<>();
/**
* 未解析前搜索到的
*/
private ArrayList<NsdServiceInfo> mNsdServiceInfoListBefore = new ArrayList<>();
private static final int MSG_RESOLVER = 1002;
private static final int MSG_NULL = 1003;
int count;
/**
* @param context this
* @param serviceName 客户端扫描 指定的地址 暂时没用到
* @param iServerFound 回调
*/
public NsdClient(Context context, String serviceName, IServerFound iServerFound) {
mContext = context;
mServiceName = serviceName;
mIServerFound = iServerFound;
}
public void startNSDClient() {
mNsdManager = (NsdManager) mContext.getSystemService(Context.NSD_SERVICE);
initializeDiscoveryListener();
mNsdManager.discoverServices(NSD_SERVER_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
initializeResolveListener();
}
/**
* 扫描未被解析前的 NsdServiceInfo
* 用于服务发现的回调调用接口
*/
private void initializeDiscoveryListener() {
mDiscoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
mNsdManager.stopServiceDiscovery(this);
Log.e(TAG, "onStartDiscoveryFailed():");
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
mNsdManager.stopServiceDiscovery(this);
Log.e(TAG, "onStopDiscoveryFailed():");
}
@Override
public void onDiscoveryStarted(String serviceType) {
Log.e(TAG, "onDiscoveryStarted():");
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.e(TAG, "onDiscoveryStopped():");
}
/**
*
* @param serviceInfo
*/
@Override
public void onServiceFound(NsdServiceInfo serviceInfo) {
//根据咱服务器的定义名称,指定解析该 NsdServiceInfo
if (serviceInfo.getServiceName().equals(mServiceName)) {
mNsdManager.resolveService(serviceInfo, mResolverListener);
} else {
mHandler.sendEmptyMessage(MSG_NULL);
}
Log.e(TAG, "onServiceFound():");
mNsdServiceInfoListBefore.add(serviceInfo);
}
@Override
public void onServiceLost(NsdServiceInfo serviceInfo) {
Log.e(TAG, "onServiceLost(): serviceInfo=" + serviceInfo);
}
};
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_RESOLVER:
//回调到主线 进行解析結果的回調
NsdServiceInfo serviceInfo = (NsdServiceInfo) msg.obj;
if (mIServerFound != null) {
mIServerFound.onServerFound(serviceInfo, serviceInfo.getPort());
}
Log.e(TAG, " 指定onServiceFound" + mServiceName + ") Service Info: --> " + serviceInfo);
break;
case MSG_NULL:
if (mIServerFound != null) {
mIServerFound.onServerFail();
}
break;
default:
}
}
};
/**
* 解析未 调用未被解析的 NsdServiceInfo
*/
private void initializeResolveListener() {
mResolverListener = new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
}
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
int port = serviceInfo.getPort();
InetAddress host = serviceInfo.getHost();
String serviceName = serviceInfo.getServiceName();
String hostAddress = serviceInfo.getHost().getHostAddress();
Log.i(TAG, "onServiceResolved 已解析:" + " host:" + hostAddress + ":" + port + " ----- serviceName: " + serviceName);
mNsdServiceInfoList.add(serviceInfo);
//解析的结果 通过Handler发送到主线程
Message msg = Message.obtain();
msg.what = MSG_RESOLVER;
msg.obj = serviceInfo;
mHandler.sendMessageDelayed(msg, 500);
}
};
}
public void stopNSDServer() {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
}
public interface IServerFound {
// void onServerFound(InetAddress host, int port);
/**
* 回調 指定解析的结果
*/
void onServerFound(NsdServiceInfo serviceInfo, int port);
// void onServerFoundList(ArrayList<NsdServiceInfo> NsdServiceInfoList);
/**
* 無合適 回調失敗
*/
void onServerFail();
}
}

View File

@@ -0,0 +1,128 @@
package com.mogo.telematic.client.handler;
import android.text.TextUtils;
import android.util.Log;
import com.mogo.telematic.client.listener.NettyClientListener;
import com.mogo.telematic.client.status.ConnectState;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
public class NettyClientHandler extends SimpleChannelInboundHandler<String> {
private static final String TAG = "NettyClientHandler";
private final boolean isSendheartBeat;
private NettyClientListener listener;
private int index;
private Object heartBeatData;
private String packetSeparator;
// private static final ByteBuf HEARTBEAT_SEQUENCE = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Heartbeat"+System.getProperty("line.separator"),
// CharsetUtil.UTF_8));
byte[] requestBody = {(byte) 0xFE, (byte) 0xED, (byte) 0xFE, 5, 4, (byte) 0xFF, 0x0a};
public NettyClientHandler(NettyClientListener listener, int index, boolean isSendheartBeat, Object heartBeatData) {
this(listener,index,isSendheartBeat,heartBeatData,null);
}
public NettyClientHandler(NettyClientListener listener, int index, boolean isSendheartBeat, Object heartBeatData,String separator) {
this.listener = listener;
this.index = index;
this.isSendheartBeat = isSendheartBeat;
this.heartBeatData = heartBeatData;
this.packetSeparator = TextUtils.isEmpty(separator) ? System.getProperty("line.separator") : separator;
}
/**
* <p>设定IdleStateHandler心跳检测每x秒进行一次读检测
* 如果x秒内ChannelRead()方法未被调用则触发一次userEventTrigger()方法 </p>
*
* @param ctx ChannelHandlerContext
* @param evt IdleStateEvent
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
if (event.state() == IdleState.WRITER_IDLE) { //发送心跳
// ctx.channel().writeAndFlush("Heartbeat" + System.getProperty("line.separator"));
if (isSendheartBeat) {
if (heartBeatData == null) {
ctx.channel().writeAndFlush("Heartbeat" + packetSeparator);
} else {
if (heartBeatData instanceof String) {
// Log.d(TAG, "userEventTriggered: String");
ctx.channel().writeAndFlush(heartBeatData + packetSeparator);
} else if (heartBeatData instanceof byte[]) {
// Log.d(TAG, "userEventTriggered: byte");
ByteBuf buf = Unpooled.copiedBuffer((byte[]) heartBeatData);
ctx.channel().writeAndFlush(buf);
} else {
Log.e(TAG, "userEventTriggered: heartBeatData type error");
}
}
} else {
Log.e(TAG, "不发送心跳");
}
}
}
}
/**
* <p>客户端上线</p>
*
* @param ctx ChannelHandlerContext
*/
@Override
public void channelActive(ChannelHandlerContext ctx) {
Log.e(TAG, "channelActive");
// NettyTcpClient.getInstance().setConnectStatus(true);
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_SUCCESS, index);
}
/**
* <p>客户端下线</p>
*
* @param ctx ChannelHandlerContext
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) {
Log.e(TAG, "channelInactive");
// NettyTcpClient.getInstance().setConnectStatus(false);
// listener.onServiceStatusConnectChanged(NettyClientListener.STATUS_CONNECT_CLOSED);
// NettyTcpClient.getInstance().reconnect();
}
/**
* 客户端收到消息
*
* @param channelHandlerContext ChannelHandlerContext
* @param msg 消息
*/
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) {
Log.e(TAG, "channelRead0:"+msg);
listener.onMessageResponseClient(msg, index);
}
/**
* @param ctx ChannelHandlerContext
* @param cause 异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
// NettyTcpClient.getInstance().setConnectStatus(false);
Log.e(TAG, "exceptionCaught");
listener.onClientStatusConnectChanged(ConnectState.STATUS_CONNECT_ERROR, index);
cause.printStackTrace();
ctx.close();
}
}

View File

@@ -0,0 +1,9 @@
package com.mogo.telematic.client.listener;
/**
* 发送状态监听
*/
public interface MessageStateListener {
void isSendSuccss(boolean isSuccess);
}

View File

@@ -0,0 +1,22 @@
package com.mogo.telematic.client.listener;
/**
* TCP状态变化监听
*/
public interface NettyClientListener<T> {
/**
* 当接收到系统消息
* @param msg 消息
* @param index tcp 客户端的标识,因为一个应用程序可能有很多个长链接
*/
void onMessageResponseClient(T msg, int index);
/**
* 当服务状态发生变化时触发
* @param statusCode 状态变化
* @param index tcp 客户端的标识,因为一个应用程序可能有很多个长链接
*/
public void onClientStatusConnectChanged(int statusCode, int index);
}

View File

@@ -0,0 +1,12 @@
package com.mogo.telematic.client.status;
/**
* 连接状态
*/
public class ConnectState {
public final static int STATUS_CONNECT_SUCCESS = 1;
public final static int STATUS_CONNECT_CLOSED = 0;
public final static int STATUS_CONNECT_ERROR = -1;
}

View File

@@ -0,0 +1,99 @@
package com.mogo.telematic.server;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.util.Log;
public class NSDServer {
public static final String TAG = "NSDServer";
private NsdManager mNsdManager;
private NsdManager.RegistrationListener mRegistrationListener;
private String mServerName;
private Context mContext;
private int mPort;
private String mServiceName;
private final String mServerType = "_http._tcp."; // 服务器type要客户端扫描服务器的一致
public NSDServer() {
}
public void startNSDServer(Context context, String serviceName, int port) {
initializeRegistrationListener();
registerService(context, serviceName, port);
}
//实例化注册监听器
private void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "NsdServiceInfo onRegistrationFailed");
if (registerState != null) {
registerState.onRegistrationFailed(serviceInfo, errorCode);
}
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.i(TAG, "onUnregistrationFailed serviceInfo: " + serviceInfo + " ,errorCode:" + errorCode);
if (registerState != null) {
registerState.onUnregistrationFailed(serviceInfo, errorCode);
}
}
@Override
public void onServiceRegistered(NsdServiceInfo serviceInfo) {
mServerName = serviceInfo.getServiceName();
Log.i(TAG, "onServiceRegistered: " + serviceInfo.toString());
Log.i(TAG, "mServerName onServiceRegistered: " + mServerName);
if (registerState != null) {
registerState.onServiceRegistered(serviceInfo);
}
}
@Override
public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
Log.i(TAG, "onServiceUnregistered serviceInfo: " + serviceInfo);
if (registerState != null) {
registerState.onServiceUnregistered(serviceInfo);
}
}
};
}
private void registerService(Context context, String serviceName, int port) {
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName(serviceName);
serviceInfo.setPort(port);
serviceInfo.setServiceType(mServerType);
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
public void stopNSDServer() {
mNsdManager.unregisterService(mRegistrationListener);
}
//NSD服务注册监听接口
public interface IRegisterState {
void onServiceRegistered(NsdServiceInfo serviceInfo); //注册NSD成功
void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode); //注册NSD失败
void onServiceUnregistered(NsdServiceInfo serviceInfo); //取消NSD注册成功
void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode); //取消NSD注册失败
}
//NSD服务接口对象
private IRegisterState registerState;
//设置NSD服务接口对象
public void setRegisterState(IRegisterState registerState) {
this.registerState = registerState;
}
}

View File

@@ -0,0 +1,46 @@
package com.mogo.telematic.server.bean;
import io.netty.channel.Channel;
/**
* 客户端信息
*/
public class ClientChanel {
private String clientIp; //客户端ip
private Channel channel; //与客户端建立的通道
private String shortId; //通道的唯一标示
public ClientChanel(String clientIp, Channel channel, String shortId) {
this.clientIp = clientIp;
this.channel = channel;
this.shortId = shortId;
}
public String getClientIp() {
return clientIp;
}
public void setClientIp(String clientIp) {
this.clientIp = clientIp;
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
public String getShortId() {
return shortId;
}
public void setShortId(String shortId) {
this.shortId = shortId;
}
}