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



我通过web套接字(即没有浏览器工作)获得应用程序到应用程序的通信有一些麻烦。由于这似乎不是最常见的应用程序的web套接字,我想知道是否有人有任何经验这样做。

为什么我要使用web sockets?

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

知道为什么这个不工作吗?

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

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

尝试做一些类似于允许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();
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新