Posts: 7
Joined: Sat Nov 05, 2016 4:56 pm

Java SE + Websocket

Sun Dec 04, 2016 2:08 pm

I'm struggling with the very basics of getting a websocket connection running. All the examples are running a local server and using Java EE -project and I don't know how to get it running on Pi. I don't understand how to make the Java code to listen to a port or whatever needs to be done

I have a java example code, a class for echoing requests. Main function is like this:

Code: Select all

    public void onMessage(String message, Session session){
        System.out.println("Message from " + session.getId() + ": " + message);
        try {
        } catch (IOException ex) {
It also has the @OnOpen and @OnClose -methods.

That's all inside "". Should I have some main loop which has running in an endless while-loop? And then I'll start the main app when Raspberry starts? Or do I need some other software in between my webserver and the app I'm writing?

I'm using Apache2 as web server and proxy_wstunnel_mod to redirect the websocket traffic to where ever needed. So right now it's going from certain "//" to "//localhost:4000/".

This should be easy to Google, but with my keywords I only get the same instructions to make a Java EE -project and run it locally.

Posts: 7
Joined: Sat Nov 05, 2016 4:56 pm

Re: Java SE + Websocket

Sun Dec 04, 2016 5:09 pm

Switched to Tomcat. Now I get the examples to work. But I have no clue how to add my own application. Just put my own file in /usr/share/tomcat8/ and use URL

User avatar
Posts: 11
Joined: Tue Nov 29, 2016 5:56 pm
Contact: Website

Re: Java SE + Websocket

Sat Dec 10, 2016 12:02 am

You might wanna try the following library:

I have used it to create my GPIO monitoring app and it worked like a charm.

Posts: 1
Joined: Wed Dec 21, 2016 11:34 pm

Re: Java SE + Websocket

Thu Dec 22, 2016 1:11 am

If you don't mind changing Tomcat for something else, I would recommend Jetty. I think it's more lightweight than Tomcat or Apache httpd.

I've been using Jetty for embedding a Java web server in my application running in Raspberry Pi. There are several different handlers implemented in Jetty, including WebSockets.

The following code snippet is what I use in order to provide websocket functionality to a Jetty application (assuming WebSocketHandlerImpl is a class with websocket annotations).

Code: Select all

        WebSocketHandler wsHandler = new WebSocketHandler() {
            public void configure(WebSocketServletFactory factory) {
            	factory.setCreator(new WebSocketCreator() {					
					public Object createWebSocket(ServletUpgradeRequest arg0, ServletUpgradeResponse arg1) {
						return new WebSocketHandlerImpl(socketsPool);
The full source code is available here: ...

But if all you need is a websocket communication, Jetty may be an overkill.

Posts: 108
Joined: Mon Jan 01, 2018 2:14 pm

Re: Java SE + Websocket

Sun Nov 11, 2018 1:46 pm

can you say how did you do it?
how did you make it work?
I have install the Java-WebSocket
but can't run it
no matter what I do - I get this error :
packege org.java_websocket.WebSocket does not exist

can you explain how did you make it work?

Thanks ,

Return to “Java”