优化netty
This commit is contained in:
@@ -1,18 +0,0 @@
|
|||||||
package com.ydd.sportfaceid.config;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 全局配置常量
|
|
||||||
*
|
|
||||||
* @author peakren
|
|
||||||
* @date 05/12/2017 10:29 PM
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class GlobalConfig {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 与服务器通讯状态
|
|
||||||
*/
|
|
||||||
public static boolean isConnected = false;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@ package com.ydd.sportfaceid.netty;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.ydd.sportfaceid.config.SDAppConfig;
|
||||||
import com.ydd.sportfaceid.netty.message.DeviceType;
|
import com.ydd.sportfaceid.netty.message.DeviceType;
|
||||||
import com.ydd.sportfaceid.netty.message.HeartBeat;
|
import com.ydd.sportfaceid.netty.message.HeartBeat;
|
||||||
import com.ydd.sportfaceid.netty.message.MessageDTO;
|
import com.ydd.sportfaceid.netty.message.MessageDTO;
|
||||||
@@ -85,8 +86,8 @@ public class ClientHandler extends SimpleChannelInboundHandler<String> {
|
|||||||
private HeartBeat getHbMessage() {
|
private HeartBeat getHbMessage() {
|
||||||
HeartBeat hb = new HeartBeat();
|
HeartBeat hb = new HeartBeat();
|
||||||
hb.setDeviceName(DeviceIdUtil.generateDeviceId(mContext));
|
hb.setDeviceName(DeviceIdUtil.generateDeviceId(mContext));
|
||||||
hb.setVenueId(mContext.getTaskId()); // TODO 从上下文选择场馆
|
hb.setVenueId(SDAppConfig.getInstance().getVenueId());
|
||||||
hb.setEnterOrOut(DeviceType.ENTER); // TODO 上下文获取场馆进场或者出场
|
hb.setEnterOrOut(SDAppConfig.getInstance().getDeviceType());
|
||||||
return hb;
|
return hb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.app.Activity;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.ydd.sportfaceid.BuildConfig;
|
import com.ydd.sportfaceid.BuildConfig;
|
||||||
import com.ydd.sportfaceid.config.GlobalConfig;
|
|
||||||
import com.ydd.sportfaceid.event.MessageEvent;
|
import com.ydd.sportfaceid.event.MessageEvent;
|
||||||
import com.ydd.sportfaceid.utils.SharedPreferencesUtil;
|
import com.ydd.sportfaceid.utils.SharedPreferencesUtil;
|
||||||
|
|
||||||
@@ -33,16 +32,14 @@ import io.netty.channel.socket.nio.NioSocketChannel;
|
|||||||
*/
|
*/
|
||||||
public class ClientThread {
|
public class ClientThread {
|
||||||
|
|
||||||
private final static String TAG = "sander";
|
private final static String TAG = "ClientThread";
|
||||||
|
|
||||||
private static ClientThread instance;
|
private static ClientThread instance;
|
||||||
|
|
||||||
private volatile EventLoopGroup workerGroup;
|
private volatile EventLoopGroup workerGroup;
|
||||||
private volatile Bootstrap bootstrap;
|
private volatile Bootstrap bootstrap;
|
||||||
private volatile boolean closed = false;
|
private volatile boolean closed = false;
|
||||||
private String remoteHost;
|
private String remoteHost;
|
||||||
private int remotePort;
|
private int remotePort;
|
||||||
|
|
||||||
private Activity mContext;
|
private Activity mContext;
|
||||||
|
|
||||||
private ChannelFuture future;
|
private ChannelFuture future;
|
||||||
@@ -65,74 +62,42 @@ public class ClientThread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
closed = false;
|
|
||||||
workerGroup = new NioEventLoopGroup();
|
workerGroup = new NioEventLoopGroup();
|
||||||
bootstrap = new Bootstrap();
|
bootstrap = new Bootstrap();
|
||||||
bootstrap.group(workerGroup);
|
bootstrap.group(workerGroup);
|
||||||
bootstrap.channel(NioSocketChannel.class);
|
bootstrap.channel(NioSocketChannel.class);
|
||||||
bootstrap.option(ChannelOption.TCP_NODELAY, true);
|
bootstrap.option(ChannelOption.TCP_NODELAY, true);
|
||||||
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
|
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
|
||||||
|
|
||||||
ReConnectHandler reConnectHandler = new ReConnectHandler();
|
ReConnectHandler reConnectHandler = new ReConnectHandler();
|
||||||
ClientHandler dmClientHandler = new ClientHandler(mContext);
|
ClientHandler dmClientHandler = new ClientHandler(mContext);
|
||||||
ClientInitializer channelInitializer = new ClientInitializer(reConnectHandler, dmClientHandler);
|
ClientInitializer channelInitializer = new ClientInitializer(reConnectHandler, dmClientHandler);
|
||||||
|
|
||||||
bootstrap.handler(channelInitializer);
|
bootstrap.handler(channelInitializer);
|
||||||
doConnect();
|
doConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearFuture(){
|
|
||||||
future = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void doConnect() {
|
public void doConnect() {
|
||||||
Log.d("sander","现在开始链接了");
|
Log.d("sander","现在开始链接了");
|
||||||
if (closed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
remoteHost = SharedPreferencesUtil.getServerIp();
|
remoteHost = SharedPreferencesUtil.getServerIp();
|
||||||
remotePort = SharedPreferencesUtil.getServerPort();
|
remotePort = SharedPreferencesUtil.getServerPort();
|
||||||
if (GlobalConfig.isConnected){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Log.d("sander", "连接 = " + remoteHost + " " + remotePort);
|
Log.d("sander", "连接 = " + remoteHost + " " + remotePort);
|
||||||
future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort));
|
future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort));
|
||||||
future.addListener((ChannelFutureListener) f -> f.channel().eventLoop().schedule(() -> {
|
future.addListener((ChannelFutureListener) f -> f.channel().eventLoop().schedule(() -> {
|
||||||
if (!f.isSuccess()) {
|
if (!f.isSuccess()) {
|
||||||
if (GlobalConfig.isConnected) {
|
|
||||||
GlobalConfig.isConnected = false;
|
|
||||||
}
|
|
||||||
doConnect();
|
doConnect();
|
||||||
EventBus.getDefault().post(new MessageEvent("等待连接"));
|
EventBus.getDefault().post(new MessageEvent("等待连接"));
|
||||||
Log.e(TAG, "连接失败!");
|
Log.e(TAG, "连接失败!");
|
||||||
} else {
|
} else {
|
||||||
EventBus.getDefault().post(new MessageEvent("已连接"));
|
EventBus.getDefault().post(new MessageEvent("已连接"));
|
||||||
Log.i(TAG, "连接成功!");
|
Log.i(TAG, "连接成功!");
|
||||||
GlobalConfig.isConnected = true;
|
|
||||||
}
|
}
|
||||||
}, 2, TimeUnit.SECONDS));
|
}, 2, TimeUnit.SECONDS));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setContext(Activity context) {
|
public void setContext(Activity context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void close() {
|
|
||||||
closed = true;
|
|
||||||
workerGroup.shutdownGracefully();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void restart() {
|
|
||||||
this.remoteHost = SharedPreferencesUtil.getServerIp();
|
|
||||||
this.remotePort = SharedPreferencesUtil.getServerPort();
|
|
||||||
close();
|
|
||||||
run();
|
|
||||||
}
|
|
||||||
|
|
||||||
@ChannelHandler.Sharable
|
@ChannelHandler.Sharable
|
||||||
public class ReConnectHandler extends ChannelInboundHandlerAdapter {
|
public class ReConnectHandler extends ChannelInboundHandlerAdapter {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.os.Bundle;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
import com.ydd.sportfaceid.R;
|
import com.ydd.sportfaceid.R;
|
||||||
import com.ydd.sportfaceid.config.SDAppConfig;
|
import com.ydd.sportfaceid.config.SDAppConfig;
|
||||||
import com.ydd.sportfaceid.main.SDBaseActivity;
|
import com.ydd.sportfaceid.main.SDBaseActivity;
|
||||||
@@ -31,6 +32,8 @@ public class SDSettingActivity extends SDBaseActivity {
|
|||||||
Integer venueId = Integer.parseInt(venue_cell.getText().toString());
|
Integer venueId = Integer.parseInt(venue_cell.getText().toString());
|
||||||
DeviceType deviceType = DeviceType.valueOf(device_type_cell.getText().toString());
|
DeviceType deviceType = DeviceType.valueOf(device_type_cell.getText().toString());
|
||||||
SDAppConfig.getInstance().configSet(venueId,deviceType);
|
SDAppConfig.getInstance().configSet(venueId,deviceType);
|
||||||
|
showToast("设置成功!");
|
||||||
|
onGoToMainAction();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onGoToMainAction(){
|
public void onGoToMainAction(){
|
||||||
|
|||||||
@@ -70,13 +70,9 @@ public class SDGustInfoView extends RelativeLayout {
|
|||||||
mTimerOutput.schedule(new TimerTask() {
|
mTimerOutput.schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() { //定时器,更新日志
|
public void run() { //定时器,更新日志
|
||||||
Observable.create(new Observable.OnSubscribe<Object>() {
|
Observable.create(subscriber -> {
|
||||||
|
|
||||||
@Override
|
|
||||||
public void call(Subscriber<? super Object> subscriber) {
|
|
||||||
subscriber.onNext(null);
|
subscriber.onNext(null);
|
||||||
subscriber.onCompleted();
|
subscriber.onCompleted();
|
||||||
}
|
|
||||||
}).subscribeOn(Schedulers.io())
|
}).subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Object>() {
|
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Object>() {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user