2011-12-02 73 views
4

通過網絡套接字(即沒有瀏覽器工作)獲取應用程序到應用程序的通信時遇到了一些麻煩。由於這似乎不是網絡套接字最常用的應用程序,所以我想知道有沒有人有這樣的經驗。通過網絡套接字進行應用程序到應用程序的通信

爲什麼我要使用網絡套接字?

由於防火牆問題,我需要通過端口80/8080(我需要繼續處理其他HTTP通信,所以我不能只使用普通的TCP/IP套接字通信)。

我是如何嘗試做這項工作的?

我爲服務器和客戶端都使用Jetty 8.0。我的服務器代碼:

public class WebSocketTestServlet extends WebSocketServlet { 

    public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) { 
     return new TestWebSocket(); 
    } 

    class TestWebSocket implements WebSocket, WebSocket.OnTextMessage 
    { 
     public void onClose(int arg0, String arg1) { 
     } 

     public void onOpen(Connection arg0) { 
     } 

     public void onMessage(String messageText) { 
     } 

    } 
} 

我的客戶端代碼:

public class MyWebSocketClient{ 

    MyWebSocketClient() throws IOException 
    {  
     WebSocketClientFactory factory = new WebSocketClientFactory(); 
     try { 
      factory.start(); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 

     WebSocketClient client = factory.newWebSocketClient(); 

     WebSocket.Connection connection = client.open(new URI("ws://myserver:8080/testws"), new WebSocket.OnTextMessage() 
     { 
      public void onOpen(Connection connection) 
      { 
      } 

      public void onClose(int closeCode, String message) 
      { 
      } 

      public void onMessage(String data) 
      { 
      } 
     }).get(50, TimeUnit.SECONDS) 

    } 

我看到了什麼問題?

一個的ProtocolException

Caused by: java.net.ProtocolException: Bad response status 302 Found 
    at org.eclipse.jetty.websocket.WebSocketClientFactory$HandshakeConnection.closed(WebSocketClientFactory.java:423) 
    at org.eclipse.jetty.websocket.WebSocketClientFactory$WebSocketClientSelector.endPointClosed(WebSocketClientFactory.java:235) 
    at org.eclipse.jetty.io.nio.SelectorManager$SelectSet.destroyEndPoint(SelectorManager.java:948) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.doUpdateKey(SelectChannelEndPoint.java:523) 
    at org.eclipse.jetty.io.nio.SelectorManager$SelectSet.doSelect(SelectorManager.java:469) 
    at org.eclipse.jetty.io.nio.SelectorManager$1.run(SelectorManager.java:283) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533) 
    at java.lang.Thread.run(Thread.java:619) 

知道爲什麼這是不工作?

+0

我有完全相同的問題。你有沒有找到解決方案? –

回答

0

試圖做類似的允許WS調用一個嵌入式碼頭REST API的東西......這是我的回聲測試代碼(碼頭7),HTH


public class myApiSocketServlet extends WebSocketServlet 
{  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException 
    { 
     OutputStream responseBody = response.getOutputStream(); 
     responseBody.write("Socket API".getBytes()); 
     responseBody.close(); 
    } 

    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) 
    { 
     return new APIWebSocket(); 
    } 

    class APIWebSocket implements WebSocket, WebSocket.OnTextMessage 
    { 

     Connection connection; 

     @Override 
     public void onClose(int arg0, String arg1) 
     { 

     } 

     @Override 
     public void onOpen(Connection c) 
     { 
      connection = c;  
     } 

     @Override 
     public void onMessage(String msg) 
     { 
      try 
      { 
       this.connection.sendMessage("I received: " + msg); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

     } 
    } 
} 
3

嘗試添加「/」,在結束您的客戶代碼中的地址:

"ws://myserver:8080/testws/" 

它只是解決了我的問題。

+0

是的,它(至少對我而言)。但是,這是一個奇怪的困難.. – Miquel

+0

非常感謝,拯救我的一天.. – Sorceror

相關問題