线程中没有实例化ConnectionManager对象
线程中没有实例化ConnectionManager对象吧?通过构造者模式返回的config没有被使用
线程中没有实例化ConnectionManager对象吧?通过构造者模式返回的config没有被使用
2017-02-15
private ConnectionConfig mConfig;
private WeakReference<Context> mContext;
private NioSocketConnector mConnection;
private IoSession mSession;
private InetSocketAddress mAddress;
public ConnectionManager(ConnectionConfig config){
this.mConfig = config;
this.mContext = new WeakReference<>(config.getContext());
init();
}
private void init() {
mAddress = new InetSocketAddress(mConfig.getIp(),mConfig.getPort());
mConnection = new NioSocketConnector();
mConnection.getSessionConfig().setReadBufferSize(mConfig.getReadBufferSize());
mConnection.getFilterChain().addLast("logger",new LoggingFilter());
mConnection.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
mConnection.setHandler(new DefaultHandler(mContext.get()));
mConnection.setDefaultRemoteAddress(mAddress);
}
/**
* 外层调用取得与服务器的连接
* @return
*/
public boolean connect(){
Log.e("tag", "准备连接");
try{
ConnectFuture future = mConnection.connect();
future.awaitUninterruptibly();
mSession = future.getSession();
SessionManager.getInstance().setSeesion(mSession);
}catch (Exception e){
e.printStackTrace();
Log.e("tag", "连接失败");
return false;
}
return mSession == null ? false : true;
}
/**
* 断开连接的方法
*/
public void disConnection(){
mConnection.dispose();//Dispose后,对象都不存在了
mConnection = null;
mSession = null;
mAddress = null;
mContext = null;
}
举报