线程中没有实例化ConnectionManager对象

来源:5-1 mina客户端创建

xujin

2017-02-15 00:41

线程中没有实例化ConnectionManager对象吧?通过构造者模式返回的config没有被使用

写回答 关注

2回答

  • 香沙小熊
    2017-02-16 17:11:33

    对应着我的写,就可以了。

  • 香沙小熊
    2017-02-16 17:10:31


    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;

    }

长连接利器—网络框架解析之mina篇

apache mina框架如何在android中使用,并能通过mina与服务器进行通信

10780 学习 · 33 问题

查看课程

相似问题