Last active
May 15, 2017 03:47
-
-
Save vkostyukov/61646852524488c7f463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DelimEncoder(delim: Char) extends SimpleChannelHandler { | |
| override def writeRequested(ctx: ChannelHandlerContext, evt: MessageEvent) = { | |
| val newMessage = evt.getMessage match { | |
| case m: String => m + delim | |
| case m => m | |
| } | |
| Channels.write(ctx, evt.getFuture, newMessage, evt.getRemoteAddress) | |
| } | |
| } | |
| object StringClientPipeline extends ChannelPipelineFactory { | |
| def getPipeline = { | |
| val pipeline = Channels.pipeline() | |
| pipeline.addLast("stringEncode", new StringEncoder(CharsetUtil.UTF_8)) | |
| pipeline.addLast("stringDecode", new StringDecoder(CharsetUtil.UTF_8)) | |
| pipeline.addLast("line", new DelimEncoder('\n')) | |
| pipeline | |
| } | |
| } | |
| object Udp extends CodecFactory[String, Unit] { | |
| def server = throw new UnsupportedOperationException("clients only") | |
| def client = Function.const { | |
| new Codec[String, Unit] { | |
| def pipelineFactory = StringClientPipeline | |
| } | |
| } | |
| } | |
| val client = ClientBuilder() | |
| .codec(Udp) | |
| .channelFactory(new NioDatagramChannelFactory(Executors.newCachedThreadPool())) | |
| .hosts("127.0.0.1:2115") | |
| .build() | |
| val f = client("request") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment