Search in sources :

Example 46 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class TestABCase2 method testCase2_10.

/**
     * 10 pings
     * @throws Exception on test failure
     */
@Test
public void testCase2_10() throws Exception {
    // send 10 pings each with unique payload
    // send close
    // expect 10 pongs with our unique payload
    // expect close
    int pingCount = 10;
    List<WebSocketFrame> send = new ArrayList<>();
    List<WebSocketFrame> expect = new ArrayList<>();
    for (int i = 0; i < pingCount; i++) {
        String payload = String.format("ping-%d[%X]", i, i);
        send.add(new PingFrame().setPayload(payload));
        expect.add(new PongFrame().setPayload(payload));
    }
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect);
    }
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 47 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class TestABCase2 method testCase2_2.

/**
     * Ping with small text payload
     * @throws Exception on test failure
     */
@Test
public void testCase2_2() throws Exception {
    byte[] payload = StringUtil.getUtf8Bytes("Hello world");
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new PingFrame().setPayload(payload));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new PongFrame().setPayload(copyOf(payload)));
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect);
    }
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 48 with PingFrame

use of org.eclipse.jetty.websocket.common.frames.PingFrame in project jetty.project by eclipse.

the class WebSocketFrame method copy.

public static WebSocketFrame copy(Frame original) {
    WebSocketFrame copy;
    switch(original.getOpCode()) {
        case OpCode.BINARY:
            copy = new BinaryFrame();
            break;
        case OpCode.TEXT:
            copy = new TextFrame();
            break;
        case OpCode.CLOSE:
            copy = new CloseFrame();
            break;
        case OpCode.CONTINUATION:
            copy = new ContinuationFrame();
            break;
        case OpCode.PING:
            copy = new PingFrame();
            break;
        case OpCode.PONG:
            copy = new PongFrame();
            break;
        default:
            throw new IllegalArgumentException("Cannot copy frame with opcode " + original.getOpCode() + " - " + original);
    }
    copy.copyHeaders(original);
    ByteBuffer payload = original.getPayload();
    if (payload != null) {
        ByteBuffer payloadCopy = ByteBuffer.allocate(payload.remaining());
        payloadCopy.put(payload.slice()).flip();
        copy.setPayload(payloadCopy);
    }
    return copy;
}
Also used : PongFrame(org.eclipse.jetty.websocket.common.frames.PongFrame) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) CloseFrame(org.eclipse.jetty.websocket.common.frames.CloseFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) ByteBuffer(java.nio.ByteBuffer)

Aggregations

PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)48 Test (org.junit.Test)45 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)33 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)30 ArrayList (java.util.ArrayList)28 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)27 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)24 ByteBuffer (java.nio.ByteBuffer)19 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)19 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)17 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)14 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)4 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)4 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)4 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)3 CloseableLocalWebSocketSession (org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession)3 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)3 ListenerPingPongSocket (examples.ListenerPingPongSocket)2 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)2 FragmentExtension (org.eclipse.jetty.websocket.common.extensions.fragment.FragmentExtension)2