ClientBootstrap
A ClientBootstrap
is an easy way to bootstrap a SocketChannel
when creating network clients.
final class ClientBootstrap
Usually you re-use a ClientBootstrap
once you set it up and called connect
multiple times on it. This way you ensure that the same EventLoop
s will be shared across all your connections.
Example:
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}
let bootstrap = ClientBootstrap(group: group)
// Enable SO_REUSEADDR.
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
// always instantiate the handler _within_ the closure as
// it may be called multiple times (for example if the hostname
// resolves to both IPv4 and IPv6 addresses, cf. Happy Eyeballs).
channel.pipeline.addHandler(MyChannelHandler())
}
try! bootstrap.connect(host: "example.org", port: 12345).wait()
/* the Channel is now connected */
The connected SocketChannel
will operate on ByteBuffer
as inbound and on IOData
as outbound messages.