Skip to content

ApolloCEC/socketUtils

Repository files navigation

Socket Utils

Apollo CI Java Repository Codacy Badge Codacy Badge

Purpose

The aim of this project is to simplify the connection between the sockets of the various modules of the AFCL platform.

Pre-Requisites

  • Gradle

Installation

gradle shadowJar 

Usage

Include the generated .jar file in your project dependencies.

Example(s)

Send

// Connect to server
Socket socket = new Socket("HOST", PORT);

// Create json object
JsonObject object = createSomeJsonObject();

// Send as json string
SocketUtils.sendJsonString(socket, object.toString());

// Send as json object (by gson)
SocketUtils.sendJsonObject(socket, object);

// Send generic json type
SocketUtils.sendJsonObject(socket, new User("Stefan", "Pedratscher"));

Recv

// Start the server socket and accept a client connection
ServerSocket serverSocket = new ServerSocket(PORT);
Socket clientSocket = serverSocket.accept();

// Wait for json string from the client
String string = SocketUtils.receiveJsonString(clientSocket);

// Wait for json object (from gson) from the client
JsonObject jsonObject = SocketUtils.receiveJsonObject(clientSocket);

// Wait for generic json from the client
User customUserObject = SocketUtils.receiveJsonObject(socket, User.class);

User class

public class User {
    private final String firstName;
    private final String lastName;

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages