2012-04-11 16 views
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(); 
} 

如果我送,這是獲得廣播的正常郵件,一切正常。但是,如果我發送一個命令,像客戶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。

+0

做了一些測試,沒有if/else的東西,它似乎工作... – Aldrian 2012-04-11 12:11:24

回答

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 
    } 
+0

+1很好的捕獲。我昨天晚上看了幾個小時...... – 2012-04-14 01:41:03