Crushberry
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

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

That's all inside "EchoServer.java". Should I have some main loop which has EchoServer.java 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 "//xxx.xxx.xxx.xxx:80/ws" 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.

Crushberry
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 myjava.java file in /usr/share/tomcat8/ and use URL xxx.xxx.xxx.xxx:8080/myjava.java?

User avatar
Spajk
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:
https://github.com/TooTallNate/Java-WebSocket

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

gustavohbf
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() {
            @Override
            public void configure(WebSocketServletFactory factory) {
                //factory.register(MyWebSocketHandler.class);
            	factory.getPolicy().setIdleTimeout(Integer.MAX_VALUE);
            	factory.setCreator(new WebSocketCreator() {					
					@Override
					public Object createWebSocket(ServletUpgradeRequest arg0, ServletUpgradeResponse arg1) {
						return new WebSocketHandlerImpl(socketsPool);
					}
				});
            }            
        };
The full source code is available here:

https://github.com/gustavohbf/robotoy/b ... erver.java

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

david_1234
Posts: 68
Joined: Mon Jan 01, 2018 2:14 pm

Re: Java SE + Websocket

Sun Nov 11, 2018 1:46 pm

Spajk
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”