Search in sources :

Example 6 with CloseReason

use of javax.websocket.CloseReason in project tomcat by apache.

the class WsFrameBase method handleThrowableOnSend.

private void handleThrowableOnSend(Throwable t) throws WsIOException {
    ExceptionUtils.handleThrowable(t);
    wsSession.getLocal().onError(wsSession, t);
    CloseReason cr = new CloseReason(CloseCodes.CLOSED_ABNORMALLY, sm.getString("wsFrame.ioeTriggeredClose"));
    throw new WsIOException(cr);
}
Also used : CloseReason(javax.websocket.CloseReason)

Example 7 with CloseReason

use of javax.websocket.CloseReason in project tomcat by apache.

the class Client method sendMessage.

/**
     * Sends the given message asynchronously to the client.
     * If there is already a async sending in progress, then the message
     * will be buffered and sent when possible.<br><br>
     *
     * This method can be called from multiple threads.
     *
     * @param msg The message to send
     */
public void sendMessage(AbstractWebsocketMessage msg) {
    synchronized (messagesToSend) {
        if (!isClosing) {
            // Check if we have a Close message
            if (msg instanceof CloseWebsocketMessage) {
                isClosing = true;
            }
            if (isSendingMessage) {
                // or length(of all messages) >= 1000000 bytes.
                if (messagesToSend.size() >= 1000 || messagesToSendLength >= 1000000) {
                    isClosing = true;
                    // Discard the new message and close the session immediately.
                    CloseReason cr = new CloseReason(CloseCodes.VIOLATED_POLICY, "Send Buffer exceeded");
                    try {
                        // TODO: close() may block if the remote endpoint doesn't read the data
                        // (eventually there will be a TimeoutException). However, this method
                        // (sendMessage) is intended to run asynchronous code and shouldn't
                        // block. Otherwise it would temporarily stop processing of messages
                        // from other clients.
                        // Maybe call this method on another thread.
                        // Note that when this method is called, the RemoteEndpoint.Async
                        // is still in the process of sending data, so there probably should
                        // be another way to abort the Websocket connection.
                        // Ideally, there should be some abort() method that cancels the
                        // connection immediately...
                        session.close(cr);
                    } catch (IOException e) {
                    // Ignore
                    }
                } else {
                    // to reduce TCP overhead (using ";" as separator).
                    if (msg instanceof StringWebsocketMessage && !messagesToSend.isEmpty() && messagesToSend.getLast() instanceof StringWebsocketMessage) {
                        StringWebsocketMessage ms = (StringWebsocketMessage) messagesToSend.removeLast();
                        messagesToSendLength -= calculateMessageLength(ms);
                        String concatenated = ms.getString() + ";" + ((StringWebsocketMessage) msg).getString();
                        msg = new StringWebsocketMessage(concatenated);
                    }
                    messagesToSend.add(msg);
                    messagesToSendLength += calculateMessageLength(msg);
                }
            } else {
                isSendingMessage = true;
                internalSendMessageAsync(msg);
            }
        }
    }
}
Also used : CloseWebsocketMessage(websocket.drawboard.wsmessages.CloseWebsocketMessage) StringWebsocketMessage(websocket.drawboard.wsmessages.StringWebsocketMessage) CloseReason(javax.websocket.CloseReason) IOException(java.io.IOException)

Example 8 with CloseReason

use of javax.websocket.CloseReason in project jetty.project by eclipse.

the class AbstractJsrEventDriver method onClose.

@Override
public final void onClose(CloseInfo close) {
    if (hasCloseBeenCalled) {
        // avoid duplicate close events (possible when using harsh Session.disconnect())
        return;
    }
    hasCloseBeenCalled = true;
    CloseCode closecode = CloseCodes.getCloseCode(close.getStatusCode());
    CloseReason closereason = new CloseReason(closecode, close.getReason());
    onClose(closereason);
}
Also used : CloseReason(javax.websocket.CloseReason) CloseCode(javax.websocket.CloseReason.CloseCode)

Example 9 with CloseReason

use of javax.websocket.CloseReason in project spring-framework by spring-projects.

the class StandardWebSocketSession method close.

@Override
public Mono<Void> close(CloseStatus status) {
    try {
        CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode());
        getDelegate().close(new CloseReason(code, status.getReason()));
    } catch (IOException e) {
        return Mono.error(e);
    }
    return Mono.empty();
}
Also used : CloseReason(javax.websocket.CloseReason) IOException(java.io.IOException)

Example 10 with CloseReason

use of javax.websocket.CloseReason in project spring-framework by spring-projects.

the class StandardWebSocketHandlerAdapterTests method onClose.

@Test
public void onClose() throws Throwable {
    this.adapter.onClose(this.session, new CloseReason(CloseCodes.NORMAL_CLOSURE, "reason"));
    verify(this.webSocketHandler).afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL.withReason("reason"));
}
Also used : CloseReason(javax.websocket.CloseReason) Test(org.junit.Test)

Aggregations

CloseReason (javax.websocket.CloseReason)29 IOException (java.io.IOException)16 Session (javax.websocket.Session)8 URI (java.net.URI)7 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)5 Endpoint (javax.websocket.Endpoint)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 EndpointConfig (javax.websocket.EndpointConfig)4 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)3 UndertowSession (io.undertow.websockets.jsr.UndertowSession)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)2 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)2 FrameChecker (io.undertow.websockets.utils.FrameChecker)2 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)2 CoderResult (java.nio.charset.CoderResult)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2