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);
}
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);
}
}
}
}
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);
}
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();
}
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"));
}
Aggregations