I've tried to recreate WebSocket chat app (http://sparkjava.com/tutorials/websocket-chat) in Spark Java framework and stumbled upon a problem.
Frontend app couldn't connect to the server via WebSocket. I've looked into "Network" tab in the browser and there's a proper WebSocket call to a server but it respond with 500 code. There's no log on the server side, even though I've added System.out.println into @OnWebSocketConnect and @OnWebSocketClose functions. Why is that?
On every app launch I have an warning that says:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.... for further details.
I don't think this is the issue but it might be worth mentioning.
I've added an exception catcher to the main class too, but it doesn't fire up:
exception(Exception.class, (e, req, res) -> {
System.err.println("Exception happened");
e.printStackTrace();
});
I managed to fix the issue. The problem was trivial: My ChatWebSocketHandler had incorrect implementation of @OnWebSocketClosed which was
public void onDisconnect(Session user)
instead of
public void onDisconnect(Session user, int statusCode, String reason).
Of course, proper Exception was thrown but mine invalid configuration of SLF4J made it quiet (NOP).
I've fixed the issue, and learned that I need to add dependencies below
compile "org.slf4j:slf4j-api:1.7.13"
compile "org.slf4j:slf4j-simple:1.7.21"
to my gradle file to make SLF4J work properly.