java - 服务器不直接响应我的命令

标签 java netty

首先,我承认我对此很陌生,我可能只是忘记在正确的变量中设置一个选项,但我的谷歌搜索失败了,我不知道该怎么做,所以我希望得到一些帮助。

我基于 SecureChat 示例,它可以位于此处:http://netty.io/docs/unstable/xref/org/jboss/netty/example/securechat/package-summary.html

我所做的改变仅在 SecureChatServerHandler 中。更准确地说,在 messageRecieved block 中:

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    // Convert the message to a string
    String request = (String) e.getMessage();

    System.out.println("Message recieved: " + request);

    if (request.equalsIgnoreCase("clients")) {
        channels.write("We currently have: " + channels.size() + " clients");
    } else if (request.toLowerCase().equals("koko"))
        for (Channel c : channels) {
            if (c == e.getChannel())
                c.write("HELLO WORLD");
        }
    else {
        // Then send it to all channels, but the current one.
        for (Channel c : channels)
            if (c != e.getChannel())
                c.write("[" + e.getChannel().getRemoteAddress() + "] " + request + "\n");
            else
                c.write("[you] " + request + "\n");

    }

    if (request.equalsIgnoreCase("bye"))
        e.getChannel().close();
}

如果我发送一条正在广播的正常消息,则一切正常。但是,如果我发送命令,例如 clientskoko,我不会收到任何响应,直到我再次按 Enter 并发送一条空消息。首先我得到回复。

 C:\Device Manager\Application Server\Examp
 les\SecureChat\SecureChatClient\bin>java -jar client.jar 127.0.0.1 8080
 UNKNOWN SERVER CERTIFICATE: CN=securechat.example.netty.gleamynode.net, OU=Contr
 ibutors, O=The Netty Project, L=Seongnam-si, ST=Kyunggi-do, C=KR
 Welcome to Electus secure chat service!
 Your session is protected by TLS_DHE_RSA_WITH_AES_128_CBC_SHA cipher suite
 You are the 1th user
 koko<ENTER>
 <PRESS ENTER AGAIN>
 HELLO WORLD[you]
 clients<ENTER>
 <AND ENTER ONCE AGAIN>
 We currently have: 1 clients[you]

我不明白,也不想要的是-按两次输入按钮-的事情。这看起来非常不合逻辑而且令人恼火。我在 Telnet 示例中没有遇到这些问题。

感谢您的宝贵时间。

问候, 阿尔德里安。

最佳答案

这是那些令人羞辱的时刻之一,你只是忘记了一个小细节,结果就把一切搞乱了。

if (request.equalsIgnoreCase("clients")) {
    channels.write("We currently have: " + channels.size() + " clients /n"); // Forgot /n here
} else if (request.toLowerCase().equals("koko"))
    for (Channel c : channels) {
        if (c == e.getChannel())
            c.write("HELLO WORLD /n"); // <- Forgot /n here as well
    }

关于java - 服务器不直接响应我的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10105495/

相关文章:

java.lang.IllegalStateException : Expected to be called on the main thread but was RxCachedThreadScheduler-1 错误

java - 在客户端和服务器 Bootstrap 之间共享 EventLoopGroup

ios - Netty Channel 关闭检测

java - 并排打印一维和二维阵列

java - 将 spring bean 注入(inject)未由 spring 实例化的类中

java - 在 Accumulo 表上运行 mapreduce 作业时出现 TApplicationException 异常

java - Junit 测试用例

java - 使用 Netty 的 Swing 客户端

java - netty 4.x.x 中的 UDP 广播

java - 如何使用 littleproxy/netty 缓存流量?