Skip to content

Instantly share code, notes, and snippets.

@fbettag
Last active December 11, 2015 18:58
Show Gist options
  • Save fbettag/4645338 to your computer and use it in GitHub Desktop.
Save fbettag/4645338 to your computer and use it in GitHub Desktop.
Netty Channels used comfortable with Scala Implicits
import io.netty.util._
import io.netty.channel._
import io.netty.handler.codec.http._
import io.netty.handler.codec.http.websocketx._
package object netty {
object NettyChannelAttr {
val session = new AttributeKey[String]("Channel.session")
val date = new AttributeKey[java.util.Date]("Channel.date")
val keepAlive = new AttributeKey[Boolean]("Channel.keepAlive")
}
implicit class UberNettyContext(ctx: ChannelHandlerContext) extends ThruputNettyChannel(ctx.channel)
implicit class UberNettyChannel(ch: Channel) {
def session = Option[String](ch.attr(NettyChannelAttr.session).get)
def session_=(a: String): Unit = ch.attr(NettyChannelAttr.session).set(a)
def date = Option[java.util.Date](ch.attr(NettyChannelAttr.date).get)
def date_=(a: java.util.Date): Unit = ch.attr(NettyChannelAttr.date).set(a)
def keepAlive: Boolean = Option[Boolean](ch.attr(NettyChannelAttr.keepAlive).get) getOrElse false
def keepAlive_=(a: Boolean): Unit = ch.attr(NettyChannelAttr.keepAlive).set(a)
def answer(rsp: FullHttpResponse) {
val future = ch.write(rsp)
if (!keepAlive) future.addListener(ChannelFutureListener.CLOSE)
}
}}
if (ctx.keepAlive) println("yeah")
ctx.answer(new DefaultFullHttpResponse(..))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment