【发布时间】:2023-03-10 05:15:01
【问题描述】:
我有一个 Netty 4.0.19 应用程序,我在其中使用 TextWebSocketFrames 发送和接收消息。
这些消息在被接收时,都会通过“onmessage”事件处理程序触发。
如何在 onerror 处理程序上生成错误消息?
例如,如何将以下错误条件作为错误而不是消息发送回 Web 套接字客户端?
public void channelRead0(final ChannelHandlerContext ctx, final TextWebSocketFrame msg) throws Exception
{
final String message = msg.retain().text();
Envelope envelope = null;
boolean errorFree = true;
try
{
envelope = EnvelopeJsonEncoder.decode(MessageType.PUBLISH, message).build();
}
catch (final Exception e)
{
errorFree = false;
final WebSocketFrame outFrame = new CloseWebSocketFrame(1002, "Closing due to error");
ctx.writeAndFlush(outFrame);
}
if (errorFree)
{
ctx.fireChannelRead(envelope);
}
}
【问题讨论】: