当前位置: 首页>>代码示例>>Scala>>正文


Scala SimpleChannelInboundHandler类代码示例

本文整理汇总了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)
  }
} 
开发者ID:bwsw,项目名称:tstreams-transaction-server,代码行数:26,代码来源:ServerInitializer.scala

示例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)
  }
} 
开发者ID:madprogrammer,项目名称:scxmppd,代码行数:49,代码来源:MappedWebServerHandler.scala


注:本文中的io.netty.channel.SimpleChannelInboundHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。