手机作为服务器使用的新时代

  1. 基于Apache MINA 开源框架

  2. 实现Android OS 作为服务器、客户端,进行Socket通讯

  3. 结合HotWiFi项目,手机架设热点进行通讯

How to

  • Step 1. Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } } 
  • Step 2. Add the dependency
dependencies { compile 'com.github.cuihp:AndroidServer:-SNAPSHOT' } 
  • Step 3.初始化服务器端
 //建造者模式进行相关配置 ServerConfig serverConfig = new ServerConfig.Builder().setPort(8888).build(); MinaServer minaServer = new MinaServer(serverConfig); //服务器状态进行监听 minaServer.setServerStateListener(new MinaServer.ServerStateListener() { @Override public void sessionCreated() { Log.d(TAG, "server sessionCreated "); } @Override public void sessionOpened() { Log.d(TAG, "server sessionOpened "); } @Override public void sessionClosed() { Log.d(TAG, "server sessionClosed "); } @Override public void messageReceived(String message) { Log.d(TAG, "server messageReceived "); } @Override public void messageSent(String message) { Log.d(TAG, "server messageSent "+message); } }); } 
  • Step 4.初始化客户端
 //建造者模式进行相关配置 ClientConfig clientConfig = new ClientConfig.Builder().setIp("127.0.0.1").setPort(8888).build(); MinaClient minaClient = new MinaClient(clientConfig); //状态进行监听 minaClient.setClientStateListener(new MinaClient.ClientStateListener() { @Override public void sessionCreated() { Log.d(TAG, "client sessionCreated "); } @Override public void sessionOpened() { Log.d(TAG, "client sessionOpened "); } @Override public void sessionClosed() { Log.d(TAG, "client sessionClosed "); } @Override public void messageReceived(String message) { Log.d(TAG, "client messageReceived "+message.toString()); } @Override public void messageSent(String message) { Log.d(TAG, "client messageSent "+message); } }); 
  • Step 5.消息发送
 minaClient.sendMessage("hello server "+System.currentTimeMillis()); minaServer.sendMessage("hello client "+System.currentTimeMillis()); 
    原文作者:Android
    原文地址: https://juejin.im/entry/58f7080fa22b9d006c12168f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞