本文整理汇总了Scala中io.netty.channel.SimpleChannelInboundHandler类的典型用法代码示例。如果您正苦于以下问题:Scala SimpleChannelInboundHandler类的具体用法?Scala SimpleChannelInboundHandler怎么用?Scala SimpleChannelInboundHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SimpleChannelInboundHandler类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ServerInitializer
//设置package包名称以及导入依赖的类
package com.bwsw.tstreamstransactionserver.netty.server
import com.bwsw.tstreamstransactionserver.netty.Message
import com.bwsw.tstreamstransactionserver.options.ServerOptions.TransportOptions
import io.netty.buffer.ByteBuf
import io.netty.channel.socket.SocketChannel
import io.netty.channel.{ChannelInitializer, SimpleChannelInboundHandler}
import io.netty.handler.codec.LengthFieldBasedFrameDecoder
import io.netty.handler.codec.bytes.ByteArrayEncoder
class ServerInitializer(serverHandler: => SimpleChannelInboundHandler[ByteBuf]) extends ChannelInitializer[SocketChannel] {
override def initChannel(ch: SocketChannel): Unit = {
ch.pipeline()
.addLast(new ByteArrayEncoder())
.addLast(new LengthFieldBasedFrameDecoder(
Int.MaxValue,
//packageTransmissionOpts.maxDataPackageSize max packageTransmissionOpts.maxMetadataPackageSize,
Message.headerFieldSize,
Message.lengthFieldSize)
)
.addLast(serverHandler)
}
}
示例2: MappedWebServerHandler
//设置package包名称以及导入依赖的类
package com.scxmpp.http
import com.typesafe.config.Config
import com.scxmpp.server.ServerContext
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
import io.netty.handler.codec.http._
import scala.collection.JavaConversions._
import scala.collection.{immutable, mutable}
class MappedWebServerHandler(context: ServerContext, config: Config) extends SimpleChannelInboundHandler[FullHttpRequest] {
val handlers = mutable.HashMap.empty[String, UriBasedHandler]
if (handlers.isEmpty) {
try {
for(handler <- config.getConfigList("handlers")) {
val path = handler.getString("path")
val clazz = context.dynamicAccess.createInstanceFor[UriBasedHandler](
handler.getString("module"), immutable.Seq((classOf[Config], handler))).get
handlers += (path -> clazz)
}
} catch {
case e: Throwable => e.printStackTrace()
}
}
override def channelReadComplete(ctx: ChannelHandlerContext) {
ctx.flush
}
override def channelRead0(ctx: ChannelHandlerContext, request: FullHttpRequest) {
val path = new QueryStringDecoder(request.uri).path()
val key = path.substring(0, path.indexOf("/", 1) match { case -1 => path.length; case x => x })
val handler = handlers.get(key)
// Pass the untouched request to the handler for maximum flexibility
if (handler.isDefined)
handler.get.process(ctx, request)
else
HttpHelpers.sendError(ctx, HttpResponseStatus.NOT_FOUND)
}
override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
cause.printStackTrace()
if (ctx.channel.isActive)
HttpHelpers.sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR)
}
}