[英]How to store a userlist with username and ChannelHandlerContext in netty
i try to send an message ot an specific user in netty. 我尝试向Netty中的特定用户发送消息。 For this i need to store the username and the CahnnelHandlerContext in an Arralist, that i can check if the user is connectet to the server and get the ChannelhandlerContext for the specific user. 为此,我需要将用户名和CahnnelHandlerContext存储在Arralist中,以便可以检查用户是否已连接到服务器并获取特定用户的ChannelhandlerContext。
Can you tell me the best practice for this? 您能告诉我最佳做法吗?
Here some code of the Server where i handle the connections: 这是我处理连接的服务器的一些代码:
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.util.ArrayList;
import messages.SecureMessageServerClientMessage;
import messages.SecureMessageServerUser;
/**
* Handles a server-side channel.
*/
public class SecureChatServerHandler extends SimpleChannelInboundHandler<SecureMessageServerClientMessage> {
protected ArrayList<SecureMessageServerUser> userList = new ArrayList();
@Override
public void channelActive(final ChannelHandlerContext ctx) {
// Once session is secured, send a greeting and register the channel to the global channel
// list so the channel received the messages from others.
ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener(
new GenericFutureListener<Future<Channel>>() {
@Override
public void operationComplete(Future<Channel> future) throws Exception {
userList.add(new SecureMessageServerUser("", ctx));
}
});
}
@Override
public void channelRead0(ChannelHandlerContext ctx, SecureMessageServerClientMessage msg ) throws Exception {
switch(msg.getType()){
case 0:
ctx.writeAndFlush(new SecureMessageServerClientMessage(0, this.userList));
break;
case 1:
System.out.println("TestMessage Typ 1");
String empfaenger, sender, message;
sender=msg.getSender();
empfaenger = msg.getEmpfaenger();
message = msg.getMessage();
for (SecureMessageServerUser user : this.userList) {
System.out.println(user.getUserName());
// if (user.getUserName().equals(empfaenger)) {
// user.getCtx().writeAndFlush(new SecureMessageServerClientMessage(1, message, sender));
// }
}
Thread.sleep(1000);
break;
case 2://User aus Liste löschen und alle Resorucen freigeben
case 3: //Liste mit allen Benutzern an Clienten senden
ctx.writeAndFlush(new SecureMessageServerClientMessage(0, this.userList));
break;
default:
System.out.println("Unbekannter Nachrichtentyp");
}
}
@Override
public void channelUnregistered(final ChannelHandlerContext ctx) {
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
I think storing it in a ConcurrentMap would be the best using the username as key and the ChannelHandlerContext as value. 我认为将用户名作为键并将ChannelHandlerContext作为值存储在ConcurrentMap中是最好的。 Also note this has not really anything todo with Netty put more with what data structure to use for store a mapping from key to to value. 还要注意,与Netty并没有什么关系,要使用什么数据结构来存储从键到值的映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.