原创

netty报错websocket, is not a @Sharable handler, so can't be added or removed multiple times

1.看报错

Failed to initialize a channel. Closing: [id: 0xa8d733bc, L:/127.0.0.1:1234 - R:/127.0.0.1:49534]
io.netty.channel.ChannelPipelineException: com.core.WebSocketHandler is not a @Sharable handler, so can't be added or removed multiple times.
    at io.netty.channel.DefaultChannelPipeline.checkMultiplicity(DefaultChannelPipeline.java:601)
    at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:202)
    at io.netty.channel.DefaultChannelPipeline.addLast(DefaultChannelPipeline.java:195)
    at com.SocketInitializer.initChannel(SocketInitializer.java:37)
    at com.SocketInitializer.initChannel(SocketInitializer.java:26)
    at io.netty.channel.ChannelInitializer.initChannel(ChannelInitializer.java:129)
    at io.netty.channel.ChannelInitializer.handlerAdded(ChannelInitializer.java:112)
    at io.netty.channel.AbstractChannelHandlerContext.callHandlerAdded(AbstractChannelHandlerContext.java:964)
    at io.netty.channel.DefaultChannelPipeline.callHandlerAdded0(DefaultChannelPipeline.java:610)
    at io.netty.channel.DefaultChannelPipeline.access$100(DefaultChannelPipeline.java:46)
    at io.netty.channel.DefaultChannelPipeline$PendingHandlerAddedTask.execute(DefaultChannelPipeline.java:1461)
    at io.netty.channel.DefaultChannelPipeline.callHandlerAddedForAllHandlers(DefaultChannelPipeline.java:1126)
    at io.netty.channel.DefaultChannelPipeline.invokeHandlerAddedIfNeeded(DefaultChannelPipeline.java:651)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:503)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:416)
    at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:475)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:416)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:515)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.lang.Thread.run(Thread.java:748)

可以看出关键问题即

is not a @Sharable handler, so can't be added or removed multiple times.

2.解决

你的Handler类上加上注解 @ChannelHandler.Sharable 即可


@ChannelHandler.Sharable
public class WebSocketHandler  extends SimpleChannelInboundHandler<Object> {

    private WebSocketServerHandshaker handshaker;
        ......
        .....
}

即可

正文到此结束
本文目录