Giter Site home page Giter Site logo

nettydemo's Introduction

NettyDemo

Netty is a network application framework based on Java NIO client-server, using Netty can quickly develop network applications

> For more usage, please jump to https://github.com/netty/netty

**This project is based on the project built by Netty on the Android platform. It only provides the usage method of Netty. You can customize it according to your own needs. **

**During the demonstration, the client can modify the IP address of the TCP server in Const.java. The server can communicate with multiple clients by switching channels. **

Finally, if there are any deficiencies, please ask Haihan to raise more issues, and we will solve them together.

How to import

 dependencies {
  implementation 'com.littlegreens.netty.client:nettyclientlib:1.0.5'
 } 

1. First look at the demonstration effect, followed by a detailed usage tutorial

If it is used as a TCP client, you can directly rely on

2, HOW TO USE?

  1. Create TCP client
     NettyTcpClient mNettyTcpClient = new NettyTcpClient.Builder()
                 .setHost(Const.HOST)                       // Set server address
                 .setTcpPort(Const.TCP_PORT)                // Set the server port number
                 .setMaxReconnectTimes(5)                   // Set the maximum number of reconnections
                 .setReconnectIntervalTime(5)               // Set the reconnection interval. Unit: second
                 .setSendheartBeat(true)                    // Set whether to send heartbeat
                 .setHeartBeatInterval(5)                   // Set the heartbeat interval. Unit: second
                 .setHeartBeatData("I'm is HeartBeatData")  // Set the heartbeat data, which can be String type or byte[], whichever is set later
                 .setIndex(0)                               // Set the client ID. (Because there may be multiple tcp connections)
//               .setPacketSeparator("#")                   // Use special characters as separators to solve the problem of sticky packets. The default is to use newline characters as separators
//               .setMaxPacketLong(1024)                    // Set the maximum length of data sent once, the default is 1024
                 .build();
  1. Set up monitoring
        mNettyTcpClient.setListener(new NettyClientListener<String>() {
            @Override
            public void onMessageResponseClient(String msg, int index) {
                // Message callback from the server
            }

            @Override
            public void onClientStatusConnectChanged(int statusCode, int index) {
               // connection status callback
            }
        });
  1. connect, disconnect
  • Determine whether it is connected
mNettyTcpClient.getConnectStatus()
  • Сonnect
mNettyTcpClient.connect();
  • Disconnect
mNettyTcpClient.disconnect();
  1. ** Send information to the server **
                    mNettyTcpClient.sendMsgToServer(msg, new MessageStateListener() {
                        @Override
                        public void isSendSuccss(boolean isSuccess) {
                            if (isSuccess) {
                                Log.d(TAG, "send successful");
                            } else {
                                Log.d(TAG, "send error");
                            }
                        }
                    });

Second, the problems encountered by the small partners

  1. The message fed back by the server is truncated Answer: Since the socket will stick packets, the sdk uses a special symbol as the separator by default to solve the problem of sticking packets. When the client sends information, the sdk will add a delimiter at the end. Here you need to pay attention to the server. When returning information, the corresponding delimiter should also be added.

  2. The master branch code does not support sending byte[] format, please refer to the develop_2.0 branch for the requirement of byte[] format

nettydemo's People

Contributors

dckroff avatar

Watchers

 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.