Giter Club home page Giter Club logo

cococlientsdk-java-simple-chat-example's Introduction

cococlientsdk-java-simple-chat-example

Sample Android application to create virtual rooms for sharing information and content

setup for running this app

Get a Client ID using

  • Head to https://manage.getcoco.buzz (Sign up if needed)
  • Go to Applications tab step 1
  • Click on + Application button step 2
  • Choose the application name, group and type step 3
  • Choose the capabilities and click submit step 4
  • Click on the Created Application step 5
  • Get the client id present there step 6

Running the app

  • Replace the client_id present in strings.xml with your client id
  • Run the app

setup for other android apps

  • Copy .jar and paste it in the app/libs
  • Mark .jar as a gradle dependency
implementation fileTree(include: ['*.jar'], dir: 'libs')
  • Copy .so file directories and paste them in app/src/main/jniLibs
  • Add INTERNET permission in Manifest file
<manifest>
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>
  • Load Native Library
class Application {
  static {
    System.loadLibrary("cocojni");
  }
}
  • Init CocoClient
class Application {
  public static void main(String[] args) {
    new CocoClient.Builder()
        .addCallbackListener(new DefaultNativeCallbacksInterface() {})
        .withPlatform(new PlatformInterface() {})
        .build();
  }
}
  • Connect to a Network with invite url
class Application {
  public static void main(String[] args) {
    // Init CocoClient

    /**
     * POST: https://api.getcoco.buzz/network-manager/networks/:networkId/generate-invite
     * AUTH: Bearer Token {{access_token}}
     * BODY: {
     *        "appId": "{{client_id}}",
     *        "appCapabilities": [ 0, 1, 2 ]
     *       }
     */
    new Network.ConnectArgs()
        .setNetworkId("<NETWORK ID>")
        .setNodeId(NODE_ID)
        .setInviteURL("<INVITE URL>")
        .setNetworkName("<NETWORK NAME>")
        .setNetworkType(NETWORK_TYPE)
        .setUserRole(USER_ROLE)
        .setAccessType(ACCESS_TYPE)
        .connect();
  }
}
  • Connect to previously connected networks
class Application {
  public static void main(String[] args) {
    // Init CocoClient

    // Get the list of previously connected networks
    Network[] networks = CocoClient.getInstance().getSavedNetworks();

    for (Network network : networks) {
      network.connect();
    }
  }
}
  • Sending Data to other nodes
class Application {
  public static void main(String[] args) {
    // Init CocoClient
    // Connect to a Network

    // Get the list of previously connected networks
    Network[] networks = CocoClient.getInstance().getSavedNetworks();

    // get the network with given network id. (Null if it's not yet connected)
    Network network = CocoClient.getInstance().getNetwork("<NETWORK ID HERE>");

    // send data to all nodes in the network
    network.sendData("hello world", null);

    // send data to nodes with node id 1, 2
    network.sendData("hello world", new long[] { 1, 2 });

    // send content info to all nodes in the network
    network.sendContentInfo(System.currentTimeMillis(), "hello world", null);

    // send data to nodes with node id 1, 2
    network.sendContentInfo(System.currentTimeMillis(), "hello world", new long[] { 1, 2 });
  }
}
  • Listening for content info
class Application {
  public static void main(String[] args) {
    // Init CocoClient
    // Connect to a Network

    CocoClient.getInstance().addSubscription(new DefaultNativeCallbacksInterface() {

      @Override
      public void nodeConnectionStatusCallback(Network network, long nodeId, NodeType nodeType, boolean isOnline, Object networkContext) {
        // determines if the node is online or not
        // do something
      }

      @Override
      public void receiveDataCallback(Network network, long sourceNodeId, String data) {
        // do something
      }

      @Override
      public void contentInfoCallback(Network network, long sourceNodeId, long contentTime, String data) {
        // do something
      }
    });
  }
}
  • Disconnecting from network
class Application {
  public static void main(String[] args) {
    // Init CocoClient
    // Connect to a Network

    // Get the connected network
    Network network = CocoClient.getInstance().getNetwork("<NETWORK ID>");
    network.disconnect();
  }
}
  • Listening for connection status
class Application {
  public static void main(String[] args) {
    // Init CocoClient
    // Connect to a Network

    CocoClient.getInstance().addSubscription(new DefaultNativeCallbacksInterface() {
      @Override
      public void connectStatusCallback(Network network, Object context) {
        Network.State status = network.getState();
        // do something
      }
    });
  }
}
  • Listening for network metadata
class Application {
  public static void main(String[] args) {
    // Init CocoClient
    // Connect to a Network

    CocoClient.getInstance().addSubscription(new DefaultNativeCallbacksInterface() {
      @Override
      public void networkMetadataCallback(Network network) {
        network.getMetadata();
        // do something
      }
    });
  }
}
  • Rules for proguard and r8 located here

Gist of using the api

  class Application {

    public static void main(String[] args) {
      DefaultNativeCallbacksInterface listener;

      new CocoClient.Builder().build();
      new Network.ConnectArgs().connect();

      Network network = CocoClient.getInstance().getNetwork("<NETWORK ID>");

      CocoClient.getInstance().addSubscription(listener = new DefaultNativeCallbacksInterface() {
        @Override
        public void receiveDataCallback(Network network, long sourceNodeId, String data) {
          // listen for messages
        }
      });

      network.sendData("hello world", null);

      network.disconnect();

      CocoClient.getInstance().removeSubscription(listener);
    }
  }

NOTE

  • Some of the best practices have been omitted for sake of simplicity and readability
  • Above mentioned APIs are subject to change
  • The manual .jar and .so placement will be replaced with gradle dependency
  • NativeCallbacksInterface will be replaced with inline callback

cococlientsdk-java-simple-chat-example's People

Contributors

krishna-elear avatar elear-akshay avatar

Watchers

Ashish Bajaj 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.