Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] [HELP] How can i run WebAppContext with WebSocketHandler in same instance?

While it *is* possible to have a WebSocketHandler mixed with a WebAppContext, its not really the ideal setup.

First, here's how your specific scenario is setup ...

package jetty.websocket;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.webapp.WebAppContext;

public class MyEmbeddedServer
{
    public static void main(String[] args)
    {
        Server server = new Server(8080);
        HandlerList handlers = new HandlerList();
        server.setHandler(handlers);
        
        // Add websocket handler
        ContextHandler wshandler = new ContextHandler("/ws");
        wshandler.setHandler(new MyHandler());
        
        handlers.addHandler(wshandler);
        
        // Add web app
        WebAppContext webapp = new WebAppContext();
        webapp.setWar("src/test/wars/webapp-b.war");
        webapp.setContextPath("/app");
        
        handlers.addHandler(webapp);
        
        // Add default handler (for errors and whatnot)
        handlers.addHandler(new DefaultHandler());

        // Lets see how the server is setup after it is started
        server.setDumpAfterStart(true);
        
        try
        {
            // Start the server thread
            server.start();
            // Wait for the server thread to end
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
}

End result of this is ...

http://localhost:8080/ws/  <-- the WebSocketHandler of yours (implemented as MyHandler), only responds to websocket upgrade, 
http://localhost:8080/app/  <-- the WebAppContext

Any other requests, such as ...
http://localhost:8080/  will result in DefaultHandler showing you an error (nothing at root context)
http://localhost:8080/foo/  will result in DefaultHandler showing you an error (not a valid context)
http://localhost:8080/ws  (note the missing slash a the end), will result in a 302 redirect to http://localhost:8080/ws/

The biggest problem with this is that your WebSocketHandler and the WebAppContext cannot talk to each other directly, as they are in different/isolated classloaders.

Why not just put your websocket class into the webapp itself?
Its far easier, and you suffer no impacts on performance.

Just use a WebSocketServlet instead of a WebSocketHandler, the rest of the code should be identical.
Or, you can use the javax.websocket.* standard to implement websocket within your webapp.
(Have to use Jetty 9.2+ for that tho)




--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Tue, Jul 8, 2014 at 3:01 AM, Dmitry Polovka <messaged@xxxxxxxxxxx> wrote:
May be there is any alternative solutions? If nobody is answering…

On 5 Jul 2014, at 14:29, Dmitry Polovka <messaged@xxxxxxxxxxx> wrote:

> Hey, i’m literally 3 weeks in Java community transitioning from PHP.
> I experience problem with configuring Jetty server to run WebAppContext with WebSocketHandler.
> I tried to add WebSocketHandler to WebAppContext as parent handler, bind two handlers via HandlerCollection, but i clearly do something wrong.
> Here is my default WebAppContext configuration:
>
> public static void main(String[] args) throws Exception
> {
>       ServerConnector connector = new ServerConnector(server);
>       connector.setPort(8080);
>       server.addConnector(connector);
>
>       WebAppContext context = new WebAppContext("webapp", "/");
>
>       // Setting up browser caching. Binds params for org.eclipse.jetty.servlet.DefaultServlet.init()
>       context.getInitParams().put("org.eclipse.jetty.servlet.Default.etags", "true");
>       context.getInitParams().put("org.eclipse.jetty.servlet.Default.cacheControl", "public, max-age=0");
>
>       // Will throw an exception when will be unable to start server for some reason
>       context.setThrowUnavailableOnStartupException(true);
>
>       server.setHandler(context);
>
>       server.start();
>       server.join();
> }
>
> My goal is to add web socket support. May be to some /ws address, but on same port as main instance. Which options do I have?

_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top