Netty Websocket服务端出现unsupported message type: TextWebSocketFrame

Netty Websocket服务器如果在一定程度的并发下出现下列异常并导致channel关闭:

 java.lang.IllegalArgumentException: unsupported message type: class org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame

发生这种情况,是因为Netty Server仍处于握手状态; 在握手状态下,channel只能发送http报文,不能发送websocket帧; 如果强行发生,就会在SocketSendBufferPool.acquire()方法内抛出上述异常。

解决办法有两种:

1. 修改你的代码,确保handshake response发送完后才发出text frame

2. 回送handshake response前把websocket encoder加入到channel pipeline中,即使这时有websocket frame发出,也不会报错。– 这是作者的观点,并且他已经实现到了新版的Netty里:
https://github.com/netty/netty/commit/3b324e9515831f658fe8777df513a3a381822b34

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.