I have some problems getting communication between applications via web sockets (without browser operation). Since this is not like the usual use of web sockets, I wonder if anyone has any experience with this.
Why do I want to use web sockets?
Due to problems with the firewall, I need to go through port 80/8080 (and I need to continue to process some other HTTP messages, so I cannot just use normal TCP / IP socket communication).
How did I try to do this job?
I use Jetty 8.0 for both server and client. My server code:
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) { } } }
My client code:
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) }
What problem do I see?
AExceptionException
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)
Any idea why this is not working?
source share