This repository contains a very simple websocket server implementation in java. This implementation is 100% conformant to the websocket protocol specification RFC 6455 and passing all test cases in Autobahn|Testsuite.
It is a protocol that handles the dual communication over a single TCP connection. It is designed to use over the web. In short, it is a socket over web (http). You can read more details here.
- For those who want simplest and easiest way to use
- For those who want to learn how to build websocket server in plain java
- For those who want to run websocket server without a dedicated web server like Tomcat , Jetty (or) something else.Of course you can use this library with them as well.
- For those who want to run standalone websocket server on small computers or embedded systems like mobile phones or routers or etc..
The Autobahn|Testsuite is a fully automated test suite to verify client and server
implementations of the WebSocket Protocol
for specification conformance and implementation robustness.
Learn more about Autobahn|Test.
Creating a websocket server using our library is as simple as the following two tasks.
- Create the Endpoint class
- Register it with BasicContainer
Done
See the following piece of code that registered the Endpoint class with container
BasicContainer bc = new BasicContainer();
bc.registerEndpoint("/echo", new EchoEndpoint());
bc.listen(8080)
Here we have configure the Echopoint
to listen on 8080
port. All requests coming with ws://host:8080/echo
will be handle by the EchoEndpoint
Implementing EchoEndpoint
is also simple. Just extend Endpoint
class and override
onTextMessage
onConnect
class EchoEndpoint implements Endpoint {
private Session session;
@Override
public void onConnect(Session session) {
this.session = session;
}
@Override
public void onTextMessage(String data) {
session.getWebsocketHandler().sendMessage(data);
}
}
Full example of EchoServer
along with many others can be seen
here
Still working on it
- Java 1.8 or higher
We used gradle to build the project.So after cloning this repo, go to root of the project and run the following
cd project_root
./gradlew build
If you want to run it over TLS
to be secure so that no middle man can eavesdrop, add the key file like the following
BasicContainer bc = new BasicContainer();
bc.registerEndpoint("/", new EchoEndpoint());
setTLSKeyStore(keyFilePath, keyPass, storePass, KeyStoreType.JKS);
bc.listen(443);
see full example in examples
- JKS
- PKCS12
Free to used any part of the codes any way you want.
We valued your feedback most. Please give any kind of feedback you can. Suggestions, Issues Reporting or anything. You are more than welcome to contribute the project or report any kind of issue you faced We will fixed instantly
Report Issues Here
Contribute the project Here