Skip to content
Advertisement

Unable to implement websockets on non https server implementing springboot

I’ve been working on a project with my team for about a week and we still haven’t been able to get websockets to work. We’re running the whole server on our own machines for testing purposes and we’re unsure if it’ll be hosted on an HTTPS server in the future.

Using springboot we’ve been able to make all the basic web-site stuff work like login/registration and more, but websockets don’t seem to work…..

Here’s the code that we use:

package com.kanbanboard.websocket;





import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;


@ServerEndpoint(
        value="/events/{boardid}",
        decoders = MessageDecoder.class,
        encoders = MessageEncoder.class
)
public class WebSocketServer{

    private Session session;
    private static final Set<WebSocketServer> socketEndpoint = new CopyOnWriteArraySet<>();
    private static final HashMap<String, String> users = new HashMap<>();


    @OnOpen
    public void onOpen(Session session, @PathParam("boardid") String boardid) throws IOException, EncodeException {
        this.session = session;
        socketEndpoint.add(this);
        users.put(session.getId(), boardid);

        Message msg = new Message();
        msg.setFrom(boardid);
        msg.setContent("Connected!");
        broadcast(msg);
    }

    @OnMessage
    public void onMessage(Session session, String message) throws IOException, EncodeException {
        Message msg = new Message();
        msg.setFrom(users.get(session.getId()));
        broadcast(msg);
        System.out.println("["+session.getId()+"]: "+message);
    }

    @OnClose
    public void onClose(Session session) throws IOException, EncodeException {
        socketEndpoint.remove(this);
        Message message = new Message();
        message.setFrom(users.get(session.getId()));
        message.setContent("Disconnected!");
        broadcast(message);
        System.out.println("Connection has been  with: "+session.getId());
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        System.out.println("Error reached!!!");
            System.out.println(throwable);
    }

    private static void broadcast(Message message)
            throws IOException, EncodeException {

        socketEndpoint.forEach(endpoint -> {
            synchronized (endpoint) {
                try {
                    endpoint.session.getBasicRemote().
                            sendObject(message);
                } catch (IOException | EncodeException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

The javascript we use on the client side to test the connection:

let ws = new WebSocket("ws://localhost:8080/events/1")

ws.onopen = function(ev) {
    console.log("Opened connection")
    ws.send("Hello World")
}

What the javascript code returns:

GETws://localhost:8080/events/1
[HTTP/1.1 404  7ms]

Firefox can’t establish a connection to the server at ws://localhost:8080/events/1. debugger eval code:1:9

Yes we’ve tried it on chrome too… The thing is, when we use wss:// instead of ws:// we do get an output on intelliJ which looks like this:

java.lang.IllegalArgumentException: Invalid character found in method name [0x160x030x010x020x000x010x000x010xfc0x030x030xce0xea0x97_h0xd30xbe0xe90x080xea@0xf10xben0xdb0xf30x8cc0xd80xe30x890xfaD0xe80x1c0xb80xe80xbf0xa50x8c0xb90xc1 ]. HTTP method names must be tokens
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:419) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:271) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.63.jar:9.0.63]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

Any help or recommendations greatly appreciated.

Advertisement

Answer

Found the solution, you’re not getting it. (Just don’t use Websockets and sprinboot together, it’s not worth it.)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement