1

我有以下从我的 websocket 客户端获取的代码片段:

byte[] message = "test".getBytes();
session.getBasicRemote().sendBinary(ByteBuffer.wrap(message));

session 是 UndertowSession 并且 getBasicRemote() 返回 BasicWebSocketSessionRemoteEndpoint 对象。

调用 sendBinary() 时,该方法会抛出 BufferOverflowException 并带有以下堆栈跟踪:

java.nio.BufferOverflowException
    at java.nio.Buffer.nextPutIndex(Buffer.java:521)
    at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:297)
    at io.undertow.websockets.core.protocol.version07.WebSocket07FrameSinkChannel.createFrameHeader(WebSocket07FrameSinkChannel.java:129)
    at io.undertow.server.protocol.framed.AbstractFramedStreamSinkChannel.getFrameHeader(AbstractFramedStreamSinkChannel.java:143)
    at io.undertow.server.protocol.framed.AbstractFramedChannel.flushSenders(AbstractFramedChannel.java:623)
    at io.undertow.server.protocol.framed.AbstractFramedChannel.flush(AbstractFramedChannel.java:715)
    at io.undertow.server.protocol.framed.AbstractFramedChannel.queueFrame(AbstractFramedChannel.java:708)
    at io.undertow.server.protocol.framed.AbstractFramedStreamSinkChannel.queueFinalFrame(AbstractFramedStreamSinkChannel.java:257)
    at io.undertow.server.protocol.framed.AbstractFramedStreamSinkChannel.shutdownWrites(AbstractFramedStreamSinkChannel.java:242)
    at io.undertow.websockets.core.WebSockets.sendBlockingInternal(WebSockets.java:468)
    at io.undertow.websockets.core.WebSockets.sendBinaryBlocking(WebSockets.java:288)
    at io.undertow.websockets.jsr.WebSocketSessionRemoteEndpoint$BasicWebSocketSessionRemoteEndpoint.sendBinary(WebSocketSessionRemoteEndpoint.java:267)
    at myproject.core.server.utils.websocket.messagehandler.EchoStreamMessageHandler.onMessage(EchoStreamMessageHandler.java:49)
    at myproject.core.server.utils.websocket.messagehandler.EchoStreamMessageHandler.onMessage(EchoStreamMessageHandler.java:12)
    at io.undertow.websockets.jsr.FrameHandler$6.run(FrameHandler.java:248)
    at io.undertow.websockets.jsr.ServerWebSocketContainer.invokeEndpointMethod(ServerWebSocketContainer.java:553)
    at io.undertow.websockets.jsr.ServerWebSocketContainer.invokeEndpointMethod(ServerWebSocketContainer.java:542)
    at io.undertow.websockets.jsr.FrameHandler.invokeBinaryHandler(FrameHandler.java:216)
    at io.undertow.websockets.jsr.FrameHandler.onFullBinaryMessage(FrameHandler.java:329)
    at io.undertow.websockets.core.AbstractReceiveListener$1.complete(AbstractReceiveListener.java:130)
    at io.undertow.websockets.core.AbstractReceiveListener$1.complete(AbstractReceiveListener.java:124)
    at io.undertow.websockets.core.BufferedBinaryMessage$1.handleEvent(BufferedBinaryMessage.java:113)
    at io.undertow.websockets.core.BufferedBinaryMessage$1.handleEvent(BufferedBinaryMessage.java:98)
    at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
    at io.undertow.server.protocol.framed.AbstractFramedStreamSourceChannel$1.run(AbstractFramedStreamSourceChannel.java:273)
    at io.undertow.server.protocol.framed.AbstractFramedChannel$3.run(AbstractFramedChannel.java:232)
    at org.xnio.nio.WorkerThread.safeRun(WorkerThread.java:580)
    at org.xnio.nio.WorkerThread.run(WorkerThread.java:464)

我还尝试使用分配的 ByteBuffer 调用 sendBinary:

ByteBuffer buff = ByteBuffer.allocate(message.length);
buff.put(message);
buff.flip();
this.session.getBasicRemote().sendBinary(buff);

但得到了同样的例外。

任何想法如何解决这个问题?

谢谢!

埃尔达德

4

1 回答 1

0

Ok, Figured this out:

Other parts of my code changed Undertow's web socket buffer size to a very small number.

于 2017-04-06T08:51:25.520 回答