简体   繁体   English

如何在netty中存储具有用户名和ChannelHandlerContext的用户列表

[英]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.

相关问题 如何在 ChannelHandlerContext netty 4.1.50 中存储值 - How to store value inside ChannelHandlerContext netty 4.1.50 Netty 单元测试:如何测试作为传递的 ChannelHandlerContext 一部分的 Channel 对象的调用? - Netty unit testing: How to test invocations on the Channel object that is part of the passed ChannelHandlerContext? 将标题添加到ChannelHandlerContext(Netty)触发的响应中 - add headers to a response fired by ChannelHandlerContext (Netty) Netty ChannelHandlerContext从不同线程写入 - Netty ChannelHandlerContext writing from different thread 在netty websockets的ChannelHandlerContext中识别用户 - identify user across ChannelHandlerContext in netty websockets Java从UserList和setItems ComboBox获取用户名 - Java get UserName from UserList and setItems ComboBox Netty,如何通过所有处理程序存储数据 - Netty, how to store data through all handlers 使用Netty,如何存储每个套接字连接的状态? - With Netty, how can you store state per socket connection? 如何将Netty Channel对象存储在Redis数据库或任何其他解决方案中? - How to store netty Channel object in Redis database or any other solution? 如何以安全的方式存储用户名和密码 - How to store username and password in a secure way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM