I have written my own handler for SwiftNIO but I cannot get it to send anything.
class MyHandler: ChannelInboundHandler
{
public typealias InboundIn = ByteBuffer
public typealias OutboundOut = ByteBuffer
public func channelRead(context: ChannelHandlerContext, data: NIOAny)
{
let message = "The weather is sunny.\n"
let length = message.utf8.count
var buffer = context.channel.allocator.buffer(capacity: length)
buffer.writeString(message)
let out = self.wrapOutboundOut(buffer)
context.write(out, promise: nil)
}
public func channelReadComplete(context: ChannelHandlerContext)
{
context.flush()
}
public func errorCaught(context: ChannelHandlerContext, error: Error)
{
print("error: ", error)
context.close(promise: nil)
}
}
If I just leave the channelRead function as below it successfully echoes the incoming text:
public func channelRead(context: ChannelHandlerContext, data: NIOAny)
{
context.write(data, promise: nil)
}
What am I doing wrong?