levelrin / jws-server

A framework for WebSocket server in Java
MIT License
0 stars 0 forks source link
java websocket websockets

CircleCI Test Coverage Maven Central License

jws-server

jws stands for Java WebSocket. It is a Java library for building a WebSocket server. It supports rfc6455.

Quick Start

Put the code below on your main method.

new JwsGuide()
    .defaultServerThread()
    // default port is 42069 ;)
    .defaultPort()
    .defaultSocketThread()
    .skipHostValidation()
    .ignorePong()
    .reaction(
        // You should create your class that implements Reaction interface.
        new Reaction() {
            @Override
            public String endpoint() {
                // This reaction belongs to the '/yoi' endpoint.
                return "/yoi";
            }

            @Override
            public void onStart(final Session session) {
                // This method will be called
                // when WebSocket communication starts.
            }

            @Override
            public void onMessage(final Session session, final String message) {
                // This method will be called
                // when you receive a text message from the client.
                session.sendMessage("A message (reply) to the client.");
            }

            @Override
            public void onMessage(final Session session, final byte[] message) {
                // This method will be called
                // when you receive a message from the client in bytes.
            }

            @Override
            public void onClose(final Session session, final int code, final String reason) {
                // This method will be called
                // when WebSocket communication is about to be closed
                // for that session.
            }
        }
    ).reaction(
        // You can have multiple reactions like this.
        new MoreReaction()
    ).ready().go();

Connect to your server like this (JavaScript):

const socket = new WebSocket('ws://localhost:42069/yoi');

Dependency

You just need to add the dependency like so:

Gradle:

dependencies {
    implementation 'com.levelrin:jws-server:0.1.0'
}

Maven:

<dependency>
  <groupId>com.levelrin</groupId>
  <artifactId>jws-server</artifactId>
  <version>0.1.0</version>
</dependency>

Requirements:

  1. JDK 1.15+

How to contribute?

  1. Create a ticket.
  2. Send a pull request. Before you do that, run ./gradlew build and make sure the build is clean.