Class WebSocketServlet
- java.lang.Object
-
- javax.servlet.GenericServlet
-
- javax.servlet.http.HttpServlet
-
- org.eclipse.jetty.websocket.servlet.WebSocketServlet
-
- All Implemented Interfaces:
java.io.Serializable,javax.servlet.Servlet,javax.servlet.ServletConfig
- Direct Known Subclasses:
JettyWebSocketServlet
public abstract class WebSocketServlet extends javax.servlet.http.HttpServletAbstract Servlet used to bridge the Servlet API to the WebSocket API.To use this servlet, you will be required to register your websockets with the
WebSocketMappingso that it can create your websockets under the appropriate conditions.The most basic implementation would be as follows:
package my.example; import WebSocketServlet; import WebSocketServletFactory; public class MyEchoServlet extends WebSocketServlet { @Override public void configure(WebSocketServletFactory factory) { factory.setDefaultMaxFrameSize(4096); factory.addMapping(factory.parsePathSpec("/"), (req,res)->new EchoSocket()); } }Only request that conforms to a "WebSocket: Upgrade" handshake request will trigger the
WebSocketMappinghandling of creating WebSockets. All other requests are treated as normal servlet requests. The configuration defined by this servlet init parameters will be used as the customizer for any mappings created byWebSocketServletFactory.addMapping(PathSpec, WebSocketCreator)duringconfigure(WebSocketServletFactory)calls. The request upgrade may be peformed by this servlet, or is may be performed by aWebSocketUpgradeFilterinstance that will share the sameWebSocketMappinginstance. If the filter is used, then the filter configuraton is used as the default configuration prior to this servlets configuration being applied.Configuration / Init-Parameters:
- idleTimeout
- set the time in ms that a websocket may be idle before closing
- maxTextMessageSize
- set the size in UTF-8 bytes that a websocket may be accept as a Text Message before closing
- maxBinaryMessageSize
- set the size in bytes that a websocket may be accept as a Binary Message before closing
- inputBufferSize
- set the size in bytes of the buffer used to read raw bytes from the network layer
* - outputBufferSize
- set the size in bytes of the buffer used to write bytes to the network layer
- maxFrameSize
- The maximum frame size sent or received.
- autoFragment
- If true, frames are automatically fragmented to respect the maximum frame size.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description WebSocketServlet()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidconfigure(WebSocketServletFactory factory)Configure the WebSocketServletFactory for this servlet instance by setting default configuration (which may be overriden by annotations) and mappingWebSocketCreators.protected abstract FrameHandlerFactorygetFactory()voidinit()protected voidservice(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
-
-
-
-
Method Detail
-
configure
protected abstract void configure(WebSocketServletFactory factory)
Configure the WebSocketServletFactory for this servlet instance by setting default configuration (which may be overriden by annotations) and mappingWebSocketCreators. This method assumes a singleFrameHandlerFactorywill be available as a bean on theContextHandler, which in practise will mostly the the Jetty WebSocket API factory.- Parameters:
factory- the WebSocketServletFactory
-
getFactory
protected abstract FrameHandlerFactory getFactory()
- Returns:
- the instance of
FrameHandlerFactoryto be used to create the FrameHandler
-
init
public void init() throws javax.servlet.ServletException- Overrides:
initin classjavax.servlet.GenericServlet- Throws:
javax.servlet.ServletException
-
service
protected void service(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException, java.io.IOException- Overrides:
servicein classjavax.servlet.http.HttpServlet- Throws:
javax.servlet.ServletExceptionjava.io.IOException
-
-