Communication of applications with applications through network sockets

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?

+4
source share
2 answers

Try adding "/" at the end of your address in the client code:

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

He just fixed the problem for me.

+3
source

Trying to do something similar to allow WS calls to the Jetty REST built-in API ... here is my test echo code (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(); } } } } 
0
source

Source: https://habr.com/ru/post/1384263/


All Articles