Support for websockets #279
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a functional POC/Draft for #275 .
I will start with a tiny demo (echo application using websocket).
First, in the
Applicationclass I added a new method with the signature:WebSocketHandleris an interface (functional) that contains one methodand some default methods.
To add an echo based on websocket I must write:
If you need more control you can override other methods available in
WebSocketHandler:I use a standard
Routeto serve theindex.htmlfile that contains the script block with the websocket client side:The content of
index.htmlfile is:The last step is to "inject" the
WebSocketFilterin the Pippo launcher:I must admit that I don't like this manual approach but for the moment is OK. Probably the correct approach is to detect if the application contains websocket routes and only in this situation we must change automatically the
PippoFilterwithWebSocketFilter.This PR comes with:
websocketpackage inpippo-core(contains only three tiny interface and an abstract class)JettyServer(pippo-jettymodule)Below I will add some implementation details.
First, I think it's easy to add support for Undertow and Tomcat. We must implement
WebSocketFilter,WebSocketConnectionandWebSocketProcessorfor each server.I added an useful
AbstractWebSocketFilterthat contains common logic for all websocket filters.Were two option related to how to "inject" the websocket in Server:
WebSocketFilterthat extendsPippoFilterI chose the first variant because I can use the new created filter in
web.xml. With variant two, the websocket support is available only for applications that use Pippo with an embedded web server.TODO:
uriPatternas first parameter in Application.addWebSocket; now it's not possible to pass query parameters to websocket; thepathparameter is static. My idea to resolve this task is to extract from DefaultRouter the part that read the parameters from a Request in a separate class - the DefaultRouter class s a little big so Single Responsability pattern for this class is welcomeWebSocketContext.broadcastMessage(), for this we must register/unregister all WebSocketConnection(viaonOpenandonClosemethods) in a listAny advice, question is welcome.