首先,我承认我对此很陌生,我可能只是忘记在正确的变量中设置一个选项,但我的谷歌搜索失败了,我不知道该怎么做,所以我希望得到一些帮助。
我基于 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();
}
如果我发送一条正在广播的正常消息,则一切正常。但是,如果我发送命令,例如 clients 或 koko,我不会收到任何响应,直到我再次按 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/