/
SecureChatClientInitializer.scala
37 lines (30 loc) · 1.39 KB
/
SecureChatClientInitializer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.github.kxbmap.netty.example
package securechat
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.ssl.SslHandler
import io.netty.handler.codec.{Delimiters, DelimiterBasedFrameDecoder}
import io.netty.handler.codec.string.{StringEncoder, StringDecoder}
/**
* Creates a newly configured [[io.netty.channel.ChannelPipeline]] for a new channel.
*/
class SecureChatClientInitializer extends ChannelInitializer[SocketChannel] {
def initChannel(ch: SocketChannel): Unit = {
val pipeline = ch.pipeline()
// Add SSL handler first to encrypt and decrypt everything.
// In this example, we use a bogus certificate in the server side
// and accept any invalid certificates in the client side.
// You will need something more complicated to identify both
// and server in the real world.
val engine = SecureChatSslContextFactory.clientContext.createSSLEngine() <| {
_.setUseClientMode(true)
}
pipeline.addLast("ssl", new SslHandler(engine))
// On top of the SSL handler, add the text line codec.
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter():_*))
pipeline.addLast("decoder", new StringDecoder())
pipeline.addLast("encoder", new StringEncoder())
// and then business logic.
pipeline.addLast("handler", new SecureChatClientHandler())
}
}