com.sample.netty.socket.server.ServerHandlerOutbound.java Source code

Java tutorial

Introduction

Here is the source code for com.sample.netty.socket.server.ServerHandlerOutbound.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sample.netty.socket.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

/**
 *
 * @author Vitor
 */
public class ServerHandlerOutbound extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        ByteBuf encoded = ctx.alloc().buffer(4);
        encoded.writeBytes(((String) msg).getBytes());
        ctx.writeAndFlush(encoded, promise);
        System.err.println("[SERVER SEND MENSSAGE] " + msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        ctx.close();
    }
}