use of org.redkale.convert.Convert in project redkale by redkale.
the class WebSocketReadHandler method completed.
@Override
public void completed(Integer count, ByteBuffer readBuffer) {
if (count < 1) {
if (debug)
logger.log(Level.FINEST, "WebSocket(" + webSocket + ") abort on read buffer count, force to close channel, live " + (System.currentTimeMillis() - webSocket.getCreatetime()) / 1000 + " seconds");
webSocket.kill(CLOSECODE_ILLPACKET, "read buffer count is " + count);
return;
}
try {
webSocket.lastReadTime = System.currentTimeMillis();
currPackets.clear();
readBuffer.flip();
readDecode(readBuffer);
readBuffer.clear();
webSocket._channel.setReadBuffer(readBuffer);
try {
// 消息处理
for (final WebSocketPacket packet : currPackets) {
if (packet.type == FrameType.TEXT) {
try {
Convert convert = webSocket.getTextConvert();
if (restMessageConsumer != null) {
// 主要供RestWebSocket使用
restMessageConsumer.accept(webSocket, convert.convertFrom(webSocket._messageRestType, packet.getPayload()));
} else {
webSocket.onMessage(packet.getPayload() == null ? null : new String(packet.getPayload(), StandardCharsets.UTF_8), packet.last);
}
} catch (Throwable e) {
logger.log(Level.SEVERE, "WebSocket onTextMessage error (" + packet + ")", e);
}
} else if (packet.type == FrameType.BINARY) {
try {
Convert convert = webSocket.getBinaryConvert();
if (restMessageConsumer != null) {
// 主要供RestWebSocket使用
restMessageConsumer.accept(webSocket, convert.convertFrom(webSocket._messageRestType, packet.getPayload()));
} else {
webSocket.onMessage(packet.getPayload(), packet.last);
}
} catch (Throwable e) {
logger.log(Level.SEVERE, "WebSocket onBinaryMessage error (" + packet + ")", e);
}
} else if (packet.type == FrameType.PING) {
try {
webSocket.onPing(packet.getPayload());
} catch (Exception e) {
logger.log(Level.SEVERE, "WebSocket onPing error (" + packet + ")", e);
}
} else if (packet.type == FrameType.PONG) {
try {
// if (debug) logger.log(Level.FINEST, "WebSocket onMessage by PONG FrameType : " + packet);
webSocket.onPong(packet.getPayload());
} catch (Exception e) {
logger.log(Level.SEVERE, "WebSocket(" + webSocket + ") onPong error (" + packet + ")", e);
}
} else if (packet.type == FrameType.CLOSE) {
webSocket.initiateClosed = true;
if (debug)
logger.log(Level.FINEST, "WebSocket(" + webSocket + ") onMessage by CLOSE FrameType : " + packet);
webSocket.kill(CLOSECODE_CLIENTCLOSE, "received CLOSE frame-type message");
return;
} else {
logger.log(Level.WARNING, "WebSocket(" + webSocket + ") onMessage by unknown FrameType : " + packet);
webSocket.kill(CLOSECODE_ILLPACKET, "received unknown frame-type message");
return;
}
}
} catch (Throwable t) {
logger.log(Level.WARNING, "WebSocket(" + webSocket + ") onMessage error", t);
}
webSocket._channel.read(this);
} catch (Exception e) {
logger.log(Level.WARNING, "WebSocket(" + webSocket + ") onMessage by received error", e);
webSocket.kill(CLOSECODE_WSEXCEPTION, "websocket-received error");
}
}
Aggregations