Netty-based chat room (maintain and acquire client channel)

package chatroom;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

/ **
* @Author: Usher
* @Description:
* callback processing class, inheriting SimpleChannelInboundHandler to process outbound and inbound data, template design pattern, keep the main processing logic unchanged, and let the changed steps be completed through interface implementation
* /
public class ChatServerHandler extends SimpleChannelInboundHandler <String> {


public static ChannelGroup channels = new DefaultChannelGroup (GlobalEventExecutor.INSTANCE);
/ **
* When a client connects, handlerAdded will execute, record the channel of the client, and join the queue
* @param ctx
* @throws Exception
* /
@ Override
public void handlerAdded (ChannelHandlerContext ctx) throws Exception {
Channel inComing = ctx.channel (); // Get client channel
// Notify other clients that there is a newcomer to enter
for (Channel channel: channels) {
if (channel! = InComing)
channel.writeAndFlush ("[Welcome:" + inComing.remoteAddress () + "] enter the chat room! \ n");
}

channels.add (inComing); // Join the queue
}

/ **
* Disconnect
* @param ctx
* @throws Exception
* /
@Override
public void handlerRemoved (ChannelHandlerContext ctx) throws Exception {
Channel outComing = ctx.channel (); // Get client channel
// Notify other clients Someone left
for (Channel channel: channels) {
if (channel! = OutComing)
channel.writeAndFlush ("[Goodbye:]" + outComing.remoteAddress () + "Leave chat room! \ N");
}

channels.remove(outComing);
}

/ **
* Whenever there is a message written from the client
* @param channelHandlerContext
* @param s
* @throws Exception
* /
protected void channelRead0 (ChannelHandlerContext channelHandlerContext, String s) throws Exception {
Channel inComing = channelHandlerContext.channel ();

for (Channel channel : channels){
if (channel != inComing){
channel.writeAndFlush("[用户" + inComing.remoteAddress() + " 说:]" + s + "\n");
}else {
channel.writeAndFlush("[我说:]" + s + "\n");
}
}
}

/ **
* When the server listens to client activity
* @param ctx
* @throws Exception
* /
@Override
public void channelActive (ChannelHandlerContext ctx) throws Exception {
Channel inComing = ctx.channel ();
System.out.println (" ["+ inComing.remoteAddress () +"]: online ");
}

/**
* 离线
* @param ctx
* @throws Exception
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel inComing = ctx.channel();
System.out.println("[" + inComing.remoteAddress() + "]: 离线");
}

@Override
public void exceptionCaught (ChannelHandlerContext ctx, Throwable cause) throws Exception {
Channel inComing = ctx.channel ();
System.out.println (inComing.remoteAddress () + "communication exception!");
Ctx.close ();
}
}
————————————————
Copyright Statement: This article is an original article by CSDN blogger "Usher_Ou", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.
Original link: https://blog.csdn.net/usher_ou/java/article/details/80919350

Guess you like

Origin www.cnblogs.com/sidesky/p/12726638.html