Giter VIP home page Giter VIP logo

jetto-android's Introduction

jetto is open source library to develop chat apps, real-time games, social media apps etc. for mobile devices.

jetto provides real-time bi-directional & secure communication. It creates TCP/IP based encrypted communication between server and clients.

Visit jetto official website

Installation

Android jetto library can be added to project from gradle and maven by adding below codes.

Gradle

compile 'org.jetto:jetto-android:1.0.0'

Maven

<dependency>
    <groupId>org.jetto</groupId>
    <artifactId>jetto-android</artifactId>
    <version>1.0.0</version>
    <type>pom</type>
</dependency>

Start Client

  • Create an activity class on your Android project
/**
*
* @author gorkemsari
*/
public class MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
  • Add ClientListener interface to class
  • Abstract methods should be added onMessage, onStart, onStop, onError automatically depends on your IDE
  • If not added, abstract methods should be defined with @Override annotation manually
import org.jetto.listener.ClientListener;
    
/**
*
* @author gorkemsari
*/
public class MainActivity implements ClientListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //message: server message
    @Override
    public void onMessage(String message) {
        //messages from server
    }

    //id: client id
    @Override
    public void onStart(String id) {
        //connected to server and secure communication is started
        //an id is produced for client
    }

    //id: client id
    @Override
    public void onStop(String id) {
        //disconnected
    }

    //message: error message
    @Override
    public void onError(String message) {
        //some error occured during communication
    }
}
  • Add new ClientEndpoint with defined server address and port number
  • Set listener to ClientEndpoint and start client
  • Now client sends connection request to server
  • If connection request is received by server successfully, a random id is sent from server to client and new encryption key produced by Diffie-Hellman key exchange algorithm and secure communication is started between server and client. Aes encryption algorithm is used to encryption.
import org.jetto.listener.ClientListener;
import org.jetto.endpoint.ClientEndpoint;

/**
*
* @author gorkemsari
*/
public class MainActivity implements ClientListener {
    
    ClientEndpoint client;
    String serverIpAddress = "192.168.1.24";
    int serverPort = 2329;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        client = new ClientEndpoint(serverIpAddress, serverPort);
        client.setClientListener(this);
        client.start();
    }

    @Override
    public void onMessage(String message, String id) {
        
    }

    @Override
    public void onStart(String id) {
        
    }

    @Override
    public void onStop(String id) {
        
    }

    @Override
    public void onError(String message, String id) {
        
    }
}

Create Model

Models should be extended by Model class of jetto. Model class has type property for parsing processes.

import org.jetto.model.Model;

/**
*
* @author gorkemsari
*/
public class MessageModel extends Model {
    private String header;
    private String message;

    /**
    * @return the header
    */
    public String getHeader() {
        return header;
    }

    /**
    * @param header the header to set
    */
    public void setHeader(String header) {
        this.header = header;
    }

    /**
    * @return the message
    */
    public String getMessage() {
        return message;
    }

    /**
    * @param message the message to set
    */
    public void setMessage(String message) {
        this.message = message;
    }
}

Parse to Json

As mentioned above, models are extended by Model class of jetto. While setting models properties also type property of Model should be set. Models are parsed to json by toJson() method of Parser class. Parser uses gson library on background for parsing process.

import org.jetto.parser.Parser;

/**
*
* @author gorkemsari
*/
MessageModel messageModel = new MessageModel();
messageModel.setHeader("This is header");
messageModel.setMessage("Hello from Android!");
messageModel.setType(1);//setType is extended from Model class of jetto

Parser parser = new Parser();//parser class of jetto
String json = parser.toJson(messageModel);

Parse to Model

The message type can be retrieved by getType() method of Parser when a json message is received. Json messages are parsed to related model by toModel() method of Parser class according to type.

import org.jetto.parser.Parser;

/**
*
* @author gorkemsari
*/
String json = "{
        "header": "This is header",
        "message": "Hello from Android!",
        "type": "1"
    }";
Parser parser = new Parser();//parser class of jetto
int type = parser.getType(json);

switch (type) {
    case 1:  
        MessageModel messageModel = parser.toModel(json, MessageModel.class);
        ...
        break;
    case 2:  
        OtherModel otherModel = parser.toModel(json, OtherModel.class);
        ...
        break;
    default: 
        ...
        break;
}

Send Message

jetto sends all messages as byte array on background. Any format of string or json can be selected on high level usage. All messages will be caught on the onMessage method of server.

Send String Message to Server

client.write("Hello from Android!");

Send String Message to Specific Client

This message will be redirected to specific client by server on background. id is produced by server at begining of connection process and sent to client to define it as unique.

String id = "5345ud-9ur6j-dfg34-3e82gb";//specific client id
client.write("Hello from Android!", id);

Send String Message to Specific Clients

This message will be redirected to specific clients by server on background. Define id list of specific clients.

List<String> idList = new ArrayList<>();
idList.add("5345ud-9ur6j-dfg34-3e82gb");//client id
idList.add("e4t56-l9k2nb-0iplm-441e7y");//client id
idList.add("8yfd5s-33vbn6-27y5r-r5fxm");//client id

client.write("Hello from Android!", idList);

Send Json Message to Server

MessageModel messageModel = new MessageModel();
messageModel.setHeader("This is header");
messageModel.setMessage("Hello from Android!");
messageModel.setType(1);//setType is extended from Model class or jetto

Parser parser = new Parser();//parser class of jetto
client.write(parser.toJson(messageModel));

Send Json Message to Specific Client

This message will be redirected to specific client by server on background.

String id = "5345ud-9ur6j-dfg34-3e82gb";//client id

MessageModel messageModel = new MessageModel();
messageModel.setHeader("This is header");
messageModel.setMessage("Hello from Android!");
messageModel.setType(1);//setType is extended from Model class or jetto

Parser parser = new Parser();//parser class of jetto
client.write(parser.toJson(messageModel), id);

Send Json Message to Specific Clients

This message will be redirected to specific clients by server on background.

List<String> idList = new ArrayList<>();
idList.add("5345ud-9ur6j-dfg34-3e82gb");//client id
idList.add("e4t56-l9k2nb-0iplm-441e7y");//client id
idList.add("8yfd5s-33vbn6-27y5r-r5fxm");//client id

MessageModel messageModel = new MessageModel();
messageModel.setHeader("This is header");
messageModel.setMessage("Hello from Android!");
messageModel.setType(1);//setType is extended from Model class or jetto

Parser parser = new Parser();//parser class of jetto
client.write(parser.toJson(messageModel), idList);

Receive Message

jetto receives all messages as byte array on background. All messages will be caught on the onMessage method of client.

import org.jetto.parser.Parser;

/**
*
* @author gorkemsari
*/
@Override
public void onMessage(String message) {

    Parser parser = new Parser();//parser class of jetto
    int type = parser.getType(message);

    switch (type) {
        case 1:  
            MessageModel messageModel = parser.toModel(message, MessageModel.class);
            ...
            break;
        case 2:  
            OtherModel otherModel = parser.toModel(message, OtherModel.class);
            ...
            break;
        default: 
            ...
            break;
    }
}

jetto-android's People

Contributors

gorkemsari avatar

Stargazers

 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.