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.63k stars 1.56k forks source link

Custom notFound is not working if webSocket is enabled #1206

Closed fatduckling closed 3 years ago

fatduckling commented 3 years ago

Hi,

Thanks for the great library!

I think I found a bug:

The notFound("<html><body><h1>Custom 404 handling</h1></body></html>"); does not work when websocket() is used.

Here is my sample code:


import static spark.Spark.awaitInitialization;
import static spark.Spark.get;
import static spark.Spark.init;
import static spark.Spark.notFound;
import static spark.Spark.port;
import static spark.Spark.webSocket;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

public class Main {

  public static void main(final String[] args) {

    port(8080);
    webSocket("/", EchoWebSocket.class); // deleting this line would make notFound() work
    init();
    notFound("not found custom page");
    get("/hello", (request, response) -> "hello world");
    awaitInitialization();
  }
}

@WebSocket
class EchoWebSocket {

  // Store sessions if you want to, for example, broadcast a message to all users
  private static final Queue<Session> sessions = new ConcurrentLinkedQueue<>();

  @OnWebSocketConnect
  public void connected(Session session) {
    sessions.add(session);
  }

  @OnWebSocketClose
  public void closed(Session session, int statusCode, String reason) {
    sessions.remove(session);
  }

  @OnWebSocketMessage
  public void message(Session session, String message) throws IOException {
    System.out.println("Got: " + message);   // Print message
    session.getRemote().sendString(message); // and send it back
  }

}
fatduckling commented 3 years ago

Duplicate of https://github.com/perwendel/spark/issues/783

Closing