Search code examples
swift-nio

Handle Swift-nio server-side TCP connection event and send context data to client


I'm working on a COPS (rfc 2748) protocol handler. I need to be able to send a message from the server to the client immediately after a TCP connection is established. I'm not finding any API that describes this capability.

Any pointers would be greatly appreciated.


Solution

  • You can put a ChannelHandler into the pipeline of newly connected sockets using channel.pipeline.addHandler(MyHandler()) in childChannelInitializer that just sends data using context.writeAndFlush when your ChannelHandler receives the channelActive event. SwiftNIO will send this right when the TCP connection is up.