1

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

我基于 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();
}

如果我发送一条正在广播的正常消息,则一切正常。但是如果我发送一个命令,比如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 示例没有这些问题。

感谢您的时间。

问候,奥尔德里安。

4

1 回答 1

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
    }
于 2012-04-13T12:38:43.643 回答