Search in sources :

Example 16 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class WebSocketServlet method init.

/**
     * @see javax.servlet.GenericServlet#init()
     */
@Override
public void init() throws ServletException {
    try {
        WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
        String max = getInitParameter("maxIdleTime");
        if (max != null) {
            policy.setIdleTimeout(Long.parseLong(max));
        }
        max = getInitParameter("maxTextMessageSize");
        if (max != null) {
            policy.setMaxTextMessageSize(Integer.parseInt(max));
        }
        max = getInitParameter("maxBinaryMessageSize");
        if (max != null) {
            policy.setMaxBinaryMessageSize(Integer.parseInt(max));
        }
        max = getInitParameter("inputBufferSize");
        if (max != null) {
            policy.setInputBufferSize(Integer.parseInt(max));
        }
        ServletContext ctx = getServletContext();
        factory = WebSocketServletFactory.Loader.load(ctx, policy);
        configure(factory);
        factory.start();
        ctx.setAttribute(WebSocketServletFactory.class.getName(), factory);
    } catch (Exception x) {
        throw new ServletException(x);
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) ServletException(javax.servlet.ServletException) ServletContext(javax.servlet.ServletContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 17 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class WebSocketHandler method doStart.

@Override
protected void doStart() throws Exception {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    configurePolicy(policy);
    webSocketFactory = new WebSocketServerFactory(policy, getServer().getThreadPool(), bufferPool);
    addBean(webSocketFactory);
    configure(webSocketFactory);
    super.doStart();
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy)

Example 18 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class DeflateFrameExtensionTest method assertIncoming.

private void assertIncoming(byte[] raw, String... expectedTextDatas) {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    DeflateFrameExtension ext = new DeflateFrameExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(policy);
    ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
    ext.setConfig(config);
    // Setup capture of incoming frames
    IncomingFramesCapture capture = new IncomingFramesCapture();
    // Wire up stack
    ext.setNextIncomingFrames(capture);
    Parser parser = new UnitParser(policy);
    parser.configureFromExtensions(Collections.singletonList(ext));
    parser.setIncomingFramesHandler(ext);
    parser.parse(ByteBuffer.wrap(raw));
    int len = expectedTextDatas.length;
    capture.assertFrameCount(len);
    capture.assertHasFrame(OpCode.TEXT, len);
    int i = 0;
    for (WebSocketFrame actual : capture.getFrames()) {
        String prefix = "Frame[" + i + "]";
        Assert.assertThat(prefix + ".opcode", actual.getOpCode(), is(OpCode.TEXT));
        Assert.assertThat(prefix + ".fin", actual.isFin(), is(true));
        // RSV1 should be unset at this point
        Assert.assertThat(prefix + ".rsv1", actual.isRsv1(), is(false));
        Assert.assertThat(prefix + ".rsv2", actual.isRsv2(), is(false));
        Assert.assertThat(prefix + ".rsv3", actual.isRsv3(), is(false));
        ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i], StandardCharsets.UTF_8);
        Assert.assertThat(prefix + ".payloadLength", actual.getPayloadLength(), is(expected.remaining()));
        ByteBufferAssert.assertEquals(prefix + ".payload", expected, actual.getPayload().slice());
        i++;
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.websocket.common.Parser) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser)

Example 19 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class DeflateFrameExtensionTest method assertOutgoing.

private void assertOutgoing(String text, String expectedHex) throws IOException {
    WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
    DeflateFrameExtension ext = new DeflateFrameExtension();
    ext.setBufferPool(bufferPool);
    ext.setPolicy(policy);
    ExtensionConfig config = ExtensionConfig.parse("deflate-frame");
    ext.setConfig(config);
    Generator generator = new Generator(policy, bufferPool, true);
    generator.configureFromExtensions(Collections.singletonList(ext));
    OutgoingNetworkBytesCapture capture = new OutgoingNetworkBytesCapture(generator);
    ext.setNextOutgoingFrames(capture);
    Frame frame = new TextFrame().setPayload(text);
    ext.outgoingFrame(frame, null, BatchMode.OFF);
    capture.assertBytes(0, expectedHex);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) OutgoingNetworkBytesCapture(org.eclipse.jetty.websocket.common.test.OutgoingNetworkBytesCapture) Generator(org.eclipse.jetty.websocket.common.Generator)

Example 20 with WebSocketPolicy

use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.

the class EventDriverTest method wrap.

private EventDriver wrap(Object websocket) {
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    EventDriverFactory factory = new EventDriverFactory(new SimpleContainerScope(policy));
    return factory.wrap(websocket);
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)

Aggregations

WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)36 Test (org.junit.Test)26 ByteBuffer (java.nio.ByteBuffer)24 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)24 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)23 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)7 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)7 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)6 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)6 Parser (org.eclipse.jetty.websocket.common.Parser)5 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)4 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)3 Generator (org.eclipse.jetty.websocket.common.Generator)3 EventDriver (org.eclipse.jetty.websocket.common.events.EventDriver)3 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)3 Before (org.junit.Before)3 URI (java.net.URI)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 WebSocketContainerScope (org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope)2 DummyConnection (org.eclipse.jetty.websocket.common.test.DummyConnection)2