GithubHelp home page GithubHelp logo

lwsl's Introduction

Light Weight Socket Library

CircleCI GitHub release Discord

LWSL is an open source socket library made in Java using JSON packets.
It is used to create small backend applications such as verification servers, rats or other simple tasks.
Everyone can contribute by making pull requests.
If you have any issues / new features, present them in the issues tab.
For a faster support, feel free to join my discord here.

Usage

Example chat application

You can take a look at the example on this repository (LWSLChatExample).

Server

To create a server using LWSL, take a look at the example below.

socketServer = new SocketServer(25566)
                .setMaxConnections(20) // 0 for infinite
                .addConnectEvent(socket -> System.out.println(String.format("Client connected! (%s)", socket.toString())))
                .addDisconnectEvent(socket -> System.out.println(String.format("Client disconnected! (%s)", socket.toString())))
                .addPacketReceivedEvent((socket, packet) -> System.out.println(String.format("Packet received! (%s)", packet.getObject().toString())))
                .addReadyEvent(socketServ -> System.out.println(String.format("Socket server is ready for connections! (%s)", socketServ.getServerSocket().toString())))
                .addPacketSentEvent(((socketHandler, packet) -> System.out.println(String.format("Packet sent! (%s)", packet.getObject().toString()))));
socketServer.start();

Client

As for the client side, look at the following example.

SocketClient socketclient = new SocketClient("localhost", 25566)
                .addConnectEvent(onConnect -> System.out.println("Connected!"))
                .addDisconnectEvent(onDisconnect -> System.out.println("Disconnected!"))
                .addPacketReceivedEvent(((socket, packet) -> System.out.println(String.format("Received packet %s from %s.", packet.getObject().toString(), socket.getAddress()))))
                .addPacketSentEvent(((socketClient, packet) -> System.out.println(String.format("Sent packet %s to %s.", packet.getObject().toString(), socketClient.getAddress()))));
socketclient.connect();

Packets

Both the SocketClient and SocketServer can send packets by using the #sendPacket(Packet) method.
To make a custom packet, look at the example below.

public class LoginPacket extends Packet {

    public LoginPacket(String username, String password){
        getObject().put("username", username);
        getObject().put("password", password);
    }
}

That's it! To send it to the server, use the following method.

socketClient.sendPacket(new LoginPacket("Baddeveloper", "password123"));

Or for the client server side:

socketHandler.sendPacket(new LoginPacket("Baddeveloper", "password123"));

You can handle these packets by making an "OnPacketReceived" event.

.addPacketReceivedEvent(((socket, packet) -> {
    JSONObject object = packet.getObject();
        
    if(object.getString("username").equals("Baddeveloper") && object.getString("password").equals("password123"))
    	System.out.println("Logged in!");
})

Controller

The controller can be used to kick all clients, kick a specific client, get a client by its IP, send packets to all connected clients, ...
To get the controller's instance, use the following method.

socketServer.getController();

Here are a couple examples of the controller's usage.

final Controller controller = socketServer.getController();
        
controller.kickClient(controller.getClientByIP("127.0.0.1"));
        
controller.sendPacketToAll(new MessagePacket("Shutting down server!"));
controller.kickAll();

SSL/TLS

You can create an encrypted socket connection by passing an SSLContext object to the SocketClient and SocketServer constructors:

SocketServer socketServer = new SocketServer(25566, SSLContext.getDefault());
socketServer.start();
SocketClient socketclient = new SocketClient("localhost", 25566, SSLContext.getDefault())
socketclient.connect();

To create an SSLContext, you can use the SSLContextUtility in conjunction with the SSLContextConfig.

The SSLContextConfig contains several defaults, so for a standard .jks file, you only need to fill in the location (classpath or filesystem) and password.

These defaults can be overwritten for different types of keystore/truststore, such as PKCS11.

Below is an example of using the SSLContextUtility to construct an SSLContext from an SSLContextConfig:

final SSLContextConfig config = SSLContextConfig.builder()
        .keystoreLocation("keystore.jks") // This is on the classpath
        .keystorePassword("changeit")
        .truststoreLocation("/usr/local/truststore.jks") // This is on the filesystem
        .truststorePassword("changeit")
        .build();
final SSLContext sslContext = SSLContextUtility.getInstance().getSslContext(config);

Installation

If your project is using Maven or Gradle, check the tutorials below.

Maven

<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://jitpack.io</url>
	</repository>
</repositories>
<dependency>
	<groupId>com.github.StijnSimons</groupId>
	<artifactId>LWSL</artifactId>
	<version>VERSIONHERE</version>
</dependency>

Don't forget to fill in the version where "VERSIONHERE" is present.
Latest version:
GitHub release

Gradle

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
dependencies {
	implementation 'com.github.StijnSimons:LWSL:VERSIONHERE'
}

Don't forget to fill in the version where "VERSIONHERE" is present.
Latest version:
GitHub release

Contributors

lwsl's People

Contributors

arankin-irl avatar bddvlpr avatar

Watchers

 avatar  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.