perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.64k stars 1.56k forks source link

WebSockets do not work in 2.7.1, but do work in 2.5.5 #967

Closed linusheck closed 5 years ago

linusheck commented 6 years ago

Here is my websocket initialisation:

    ...
webSocket("/chatsocket", ChatSocket)
port(thisPort)

before(Filter { _, response ->
    response.header("Access-Control-Allow-Origin", "*")
})

// Returns a complete list of rooms
var allAsSendableCache = Pair(UniData.allAsSendable(), System.currentTimeMillis())
get("/allrooms") { _, response ->
    ...

Here's my dummy websocket:

@WebSocket
object ChatSocket {

    @OnWebSocketConnect
    fun connected(session: Session) = println("session connected")

    @OnWebSocketClose
    fun closed(session: Session, statusCode: Int, reason: String?) = println("closed sessions")

    @OnWebSocketMessage
    fun message(session: Session, message: String) = println("Got: $message")
}

You cannot connect to the websocket if you are running sparkjava 2.7.1. I was very confused by this, and as a last resort I changed the number down to a version in which I have already used websockets, which is 2.5.5. The websocket works perfectly fine in 2.5.5.

tipsy commented 6 years ago

Hi @glatteis,

It seems to work fine for my chat-app. Could you create a minimal broken example repo?

ebubep commented 6 years ago

Currently experiencing the same issue. When I reverted to 2.5.5 everything works excellently. What could be causing this issue in the latest 2.7 release .

linusheck commented 6 years ago

I want to add that I have not solved this issue myself. A minimal example should be pretty trivial to build, I'll see what I can do. Might take a little while as I have other priorities right now :D

mjavault commented 6 years ago

I had to same problem upgrading from 2.3 to 2.7.2 - my webapp would get a 404 trying to connect. What I noticed is that in 2.3, I had to my code setup as such: Server.java

webSocket("/logs", LogsWebSocketHandler.class);

index.html

var webSocket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/logs/");

Notice the trailing slash on /logs/ in the js code: the java code binds to /logs but js to /logs/. For some reason, that was fine before. I've updated my js code to be:

var webSocket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/logs");

and everything is working fine now. I tested on 2.5.5 as a sanity check, and both version of my js file work (js can connect using either /logs/ and /logs). But on 2.7.x, the url has to match exactly.

fquirin commented 5 years ago

I can confirm this trailing slash issue. In 2.5.4 I was using: webSocket("/chat", WebSocketHandler.class); in the server and var webSocket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/chat/"); in the client which was working fine. Then I switched to 2.8.0 (tested 2.6.0 and had the 404 issue as well) and I had to change the client to ".../chat"!

It seems to be an issue with Jetty 9.4.x: https://github.com/eclipse/jetty.project/issues/1800 I was looking for hours to solve the problem by investigating this Jetty web.xml stuff until I found this comment about the slash O_o