Git Product home page Git Product logo

androidsocketclient's Introduction

AndroidSocketClient

socket client server简易封装

Import

JitPack

Add it in your project's build.gradle at the end of repositories:

repositories {
  // ...
  maven { url "https://jitpack.io" }
}

Step 2. Add the dependency in the form

dependencies {
  compile 'com.github.vilyever:AndroidSocketClient:1.3.3'
}

Updates

  • 1.3.3

修复SocketServer启动监听回调时机问题
  • 1.3.0

修改消息收发机制
接收消息回调参数由String改为SocketResponsePacket,提供byte[]数据
获取String消息调用SocketResponsePacket.getMessage()实时获取

Usage

SocketClient socketClient = new SocketClient("192.168.1.1", 80);
socketClient.registerSocketDelegate(new SocketClient.SocketDelegate() {
    @Override
    public void onConnected(SocketClient client) {
        socketClient.send("message"); // 发送String消息,使用默认编码
        socketClient.send("message", "GBK"); // 发送String消息,使用GBK编码
        socketClient.send("message".getBytes()); // 发送byte[]消息
    }

    @Override
    public void onDisconnected(SocketClient client) {

    }

    @Override
    public void onResponse(SocketClient client, @NonNull SocketResponsePacket responsePacket) {
        byte[] data = responsePacket.getData(); // 获取byte[]数据
        String msg = responsePacket.getMessage(); // 使用默认编码获取String消息
        String msg2 = responsePacket.getMessage("GBK"); // 使用GBK编码获取String消息
    }
});

socketClient.setConnectionTimeout(1000 * 15); // 设置连接超时时长

socketClient.setHeartBeatInterval(1000 * 30); // 设置心跳包发送间隔

socketClient.setRemoteNoReplyAliveTimeout(1000 * 60); // 设置远程端在多长时间没有消息发送到本地时自动断开连接

socketClient.registerQueryResponse("query", "response"); // 设置自动应答键值对,即收到"query"时自动发送"response"

socketClient.setSupportReadLine(false); // 设置是否支持对每条消息添加换行符分割,默认为true

socketClient.setCharsetName("UTF-8"); // 设置发送String消息的默认编码

socketClient.connect(); // 连接,异步进行

socketClient.disconnect(); // 断开连接

socketClient.getState(); // 获取当前状态,Connecting, Connected, Disconnected

License

Apache License Version 2.0

androidsocketclient's People

Contributors

vilyever avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.