Skip to content

Commit

Permalink
Correctly read bytes from Netty Frames
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski committed Mar 27, 2024
1 parent 8229f8a commit b1a7293
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package sttp.tapir.server.netty.internal

import io.netty.buffer.{ByteBuf, Unpooled}
import io.netty.handler.codec.http.websocketx._
import io.netty.handler.codec.http.websocketx.{WebSocketFrame => NettyWebSocketFrame}
import sttp.ws.WebSocketFrame
import io.netty.buffer.Unpooled

object WebSocketFrameConverters {

def getBytes(buf: ByteBuf): Array[Byte] = {
val bytes = new Array[Byte](buf.readableBytes())
buf.readBytes(bytes)
bytes
}

def nettyFrameToFrame(nettyFrame: NettyWebSocketFrame): WebSocketFrame = {
nettyFrame match {
case text: TextWebSocketFrame => WebSocketFrame.Text(text.text, text.isFinalFragment, Some(text.rsv))
case close: CloseWebSocketFrame => WebSocketFrame.Close(close.statusCode, close.reasonText)
case ping: PingWebSocketFrame => WebSocketFrame.Ping(ping.content().nioBuffer().array())
case pong: PongWebSocketFrame => WebSocketFrame.Pong(pong.content().nioBuffer().array())
case _ => WebSocketFrame.Binary(nettyFrame.content().nioBuffer().array(), nettyFrame.isFinalFragment, Some(nettyFrame.rsv))
case ping: PingWebSocketFrame => WebSocketFrame.Ping(getBytes(ping.content()))
case pong: PongWebSocketFrame => WebSocketFrame.Pong(getBytes(pong.content()))
case _ => WebSocketFrame.Binary(getBytes(nettyFrame.content()), nettyFrame.isFinalFragment, Some(nettyFrame.rsv))
}
}

Expand Down

0 comments on commit b1a7293

Please sign in to comment.