}
@Override
public void handleEvent(final WebSocketChannel webSocketChannel) {
try {
StreamSourceFrameChannel frame = webSocketChannel.receive();
if (frame == null) {
webSocketChannel.resumeReceives();
return;
}
if (closeFrameReceived) {
frame.discard();
return;
}
long maxSize = maxMessageSize(session, frame.getType());
if (maxSize > 0 && frame.getPayloadSize() > maxSize) {
if (executeInIoThread) {
session.sendClose(new CloseReason(CloseReason.MSG_TOO_BIG, null), null);
} else {
session.getFrameHandlerExecutor().execute(new Runnable() {
@Override
public void run() {
session.sendClose(new CloseReason(CloseReason.MSG_TOO_BIG, null), null);
}
});
}
return;
}
// suspend the receives we will resume once we are ready
webSocketChannel.suspendReceives();
ChannelListener<StreamSourceChannel> listener;
FrameHandler handler = session.getFrameHandler();
if (handler == null) {
// no handler defined by the user use the default listener which takes care
// of echo back PING and CLOSE Frame to be RFC compliant
listener = defaultListener;
} else if (handler instanceof AssembledFrameHandler) {
listener = new AssembleFrameChannelListener(session, (AssembledFrameHandler) handler, this, frame, executeInIoThread);
} else if (handler instanceof FragmentedFrameHandler) {
listener = new FragmentedFrameChannelListener(session, (FragmentedFrameHandler) handler, this);
} else {
listener = new FrameHandlerListener(session, handler, this);
}
frame.getReadSetter().set(listener);
// wake up reads to trigger a read operation now
// TODO: Think about if this a really good idea
frame.wakeupReads();
} catch (IOException e) {
handleError(session, e);
}
}