通过Web套接字进行应用程序到应用程序的通信

时间:2011-12-02 07:20:44

标签: java jetty websocket

我在通过网络套接字(没有浏览器工作)进行应用程序到应用程序通信时遇到了一些麻烦。由于这似乎不是最常用的网络套接字应用程序,我想知道是否有人有这方面的经验。

为什么我要使用网络套接字?

由于防火墙问题,我需要通过端口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)

知道为什么这不起作用吗?

2 个答案:

答案 0 :(得分:3)

尝试在客户端代码的地址末尾添加“/”:

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

它只是为我解决了这个问题。

答案 1 :(得分:0)

尝试做类似的事情以允许WS调用嵌入式Jetty REST API ...这是我的echo测试代码(Jetty 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();
            }

        }
    }
}