Created
April 11, 2015 22:15
-
-
Save GregoryHlavac/2d910e8441f25a018dff to your computer and use it in GitHub Desktop.
Enabling websockets in spark with @davidvollmar 's socket and servlet adapters combined with an altered way to mount the rest of the handlers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package spark; | |
import org.eclipse.jetty.servlet.ServletContextHandler; | |
public class WebSocketExample | |
{ | |
public static void main(String[] args) | |
{ | |
setupSpark(); | |
Spark.get("/", (request, response) -> "Hello"); | |
initializeWebsocket(); | |
} | |
public static void setupSpark() | |
{ | |
Spark.port(8888); | |
} | |
public static void initializeWebsocket() | |
{ | |
ServletContextHandler servletContextHandler = new ServletContextHandler(null, "/", true, false); | |
servletContextHandler.addServlet(MyEchoServlet.class, "/sock"); | |
boolean ok = false; | |
while(!ok) | |
{ | |
ok = WebSockets.addExternalHandler(servletContextHandler); | |
} | |
System.out.println("WebSocket Handler Initialized"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package spark; | |
import org.apache.commons.lang3.reflect.FieldUtils; | |
import org.eclipse.jetty.server.Handler; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.handler.HandlerList; | |
import spark.webserver.SparkServer; | |
import java.util.ArrayList; | |
import java.util.List; | |
public final class WebSockets | |
{ | |
public static boolean addExternalHandler(Handler handler) | |
{ | |
List<Handler> h = new ArrayList<>(); | |
h.add(handler); | |
return addExternalHandlers(h); | |
} | |
public static boolean addExternalHandlers(List<Handler> handlers) | |
{ | |
SparkServer ss = Spark.server; | |
if(ss != null) | |
{ | |
Server js = null; | |
try | |
{ | |
js = (Server) FieldUtils.readDeclaredField(ss, "server", true); | |
} | |
catch (IllegalAccessException e) | |
{ | |
// TODO: Nope Nope Nope | |
} | |
if(js != null) | |
{ | |
Handler raw_handler = js.getHandler(); | |
// Already a handler list. | |
if(raw_handler instanceof HandlerList) | |
{ | |
HandlerList hl = (HandlerList) raw_handler; | |
if(hl != null) | |
{ | |
for(Handler h : handlers) | |
hl.addHandler(h); | |
return true; | |
} | |
} | |
else | |
{ | |
try | |
{ | |
Handler jettyHandler = (Handler) FieldUtils.readDeclaredField(ss, "handler", true); | |
if(jettyHandler != null) | |
{ | |
List<Handler> handlersInList = new ArrayList<>(); | |
// External Handlers First | |
handlersInList.addAll(handlers); | |
handlersInList.add(jettyHandler); | |
HandlerList newHandlers = new HandlerList(); | |
newHandlers.setHandlers(handlersInList.toArray(new Handler[handlersInList.size()])); | |
js.setHandler(newHandlers); | |
return true; | |
} | |
} | |
catch (IllegalAccessException e) | |
{ | |
// TODO: nope Nope nope. :D | |
} | |
} | |
} | |
} | |
return false; | |
} | |
} |
Is this a functional websocket example for spark?
Where is the MyEchoServlet
class?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't know, did this a while ago because the existing snippet to hack in didn't work.