}
public boolean doOnMessage(Message<?> message) {
Assert.isInstanceOf(WebSocketFrame.class, message.getPayload());
WebSocketFrame payload = (WebSocketFrame) message.getPayload();
WebSocketState state = getState(message);
if (logger.isTraceEnabled()) {
logger.trace(state);
}
if (payload.getRsv() > 0) {
if (logger.isDebugEnabled()) {
logger.debug("Reserved bits:" + payload.getRsv());
}
this.protocolViolation(message);
}
else if (payload.getType() == WebSocketFrame.TYPE_CLOSE) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Close, status:" + payload.getStatus());
}
// If we initiated the close, just close.
if (!state.isCloseInitiated()) {
if (payload.getStatus() < 0) {
payload.setStatus((short) 1000);
}
this.send(message);
}
WebSocketEvent event = new WebSocketEvent(this.getTheConnection(),
WebSocketEventType.WEBSOCKET_CLOSED, state.getPath(), state.getQueryString());
this.getTheConnection().publishEvent(event);
this.close();
}
catch (Exception e) {
throw new MessageHandlingException(message, "Send failed", e);
}
}
else if (state == null || state.isCloseInitiated()) {
if (logger.isWarnEnabled()) {
logger.warn("Message dropped - close initiated:" + message);
}
}
else if ((payload.getType() & 0xff) == WebSocketFrame.TYPE_INVALID) {
if (logger.isDebugEnabled()) {
logger.debug("Invalid:" + payload.getPayload());
}
this.protocolViolation(message);
}
else if (payload.getType() == WebSocketFrame.TYPE_FRAGMENTED_CONTROL) {
if (logger.isDebugEnabled()) {
logger.debug("Fragmented Control Op");
}
this.protocolViolation(message);
}
else if (payload.getType() == WebSocketFrame.TYPE_PING) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Ping received on " + this.getConnectionId() + ":"
+ new String(payload.getBinary(), "UTF-8"));
}
if (payload.getBinary().length > 125) {
this.protocolViolation(message);
}
else {
WebSocketFrame pong = new WebSocketFrame(WebSocketFrame.TYPE_PONG, payload.getBinary());
this.send(MessageBuilder.withPayload(pong)
.copyHeaders(message.getHeaders())
.build());
}
}
catch (Exception e) {
throw new MessageHandlingException(message, "Send failed", e);
}
}
else if (payload.getType() == WebSocketFrame.TYPE_PONG) {
if (logger.isDebugEnabled()) {
logger.debug("Pong received on " + this.getConnectionId());
}
}
else if (this.shook) {
return super.onMessage(message);
}
else {
try {
doHandshake(payload, message.getHeaders());
this.shook = true;
WebSocketEvent event = new WebSocketEvent(this.getTheConnection(),
WebSocketEventType.HANDSHAKE_COMPLETE, state.getPath(), state.getQueryString());
this.getTheConnection().publishEvent(event);
}
catch (Exception e) {
throw new MessageHandlingException(message, "Handshake failed", e);
}