jws stands for Java WebSocket. It is a Java library for building a WebSocket server. It supports rfc6455.
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');
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:
./gradlew build
and make sure the build is clean.