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

时间:2012-04-11 12:02:01

标签: java netty

首先,我承认我是新手,我可能只是忘记在某个地方设置一个选项到正确的变量,但我的谷歌搜索让我失望了,我不知道该怎么做,所以我我希望得到一些帮助。

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

我所做的不同之处仅在于SecureChatServerHandler。更准确地说,在messageRecieved块中:

@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();
}

如果我发送正在播放的正常消息,一切正常。但是,如果我发送命令,例如客户端 koko ,我就会得不到任何回应,直到我再次按回车并发送空消息。首先,我得到了回复。

 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示例中的这些问题。

感谢您的时间。

此致 Aldrian。

1 个答案:

答案 0 :(得分:1)

这是一个令人羞辱的时期,你只是忘记了一个小细节,这让一切都搞砸了。

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
    }