首先,我承认我对此并不陌生,我可能只是忘记为正确的变量在某个位置设置选项,但是我的Google搜索失败了,我也不知道该怎么做,所以我希望得到一些帮助。
我基于SecureChat示例,它可以位于此处:http://netty.io/docs/unstable/xref/org/jboss/netty/example/securechat/package-summary.html
而我所做的改变仅在SecureChatServerHandler中。更确切地说,在messageReceiveved块中:
@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之类的命令,则不会得到任何响应,直到再次按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]
我不了解,也不想,是两次按下Enter键的事情。这似乎很不合逻辑,而且很烦人。 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
}