1

First time using websockets. I have two machines that need to communicate using them. The server works fine, if I send a message with Postman, it replies correctly. For the client I used one of the examples I found, like this. But in the client, when I create the WebsocketClientEndpoint :

final WebsocketClientEndpoint clientEndPoint = 
    new WebsocketClientEndpoint(new URI("ws://myserver.com/endpoint"));

it calls the onOpen and immediately after the onClose, returning a 1011 close reason, that I read is an unexpected condition in the server.

I would need some clue to analyse what can be happening, because as I said, the server replies well in Postman. The url is the same, of course. The examples I find are quite identical, and I am not doing anything different. Any idea?

Daniel
  • 1,426
  • 1
  • 11
  • 24
Ángel Fas
  • 353
  • 1
  • 3
  • 15
  • 1
    The server is returning 1011 as the close reason. If it isn't a documented WebSocket feature it is specific to the server, and you will have to discover why it was sent. – user207421 Mar 27 '22 at 03:36
  • Thanks a lot, that put me on the track. As the server was working I focused on the client and didn't think of checking in the server how the JSON was really arriving when sent by the client instead of directly with Postman. I discovered that, even I was using the same data, when sending the JSON to the client, it was forwarding it to the server with a missing name/value pair. Quite a facepalm moment. – Ángel Fas Mar 27 '22 at 12:41

2 Answers2

0

My fault. As indicated by user207421, I should have checked what was really arriving to the server. The client was not sending hardcoded data. I was sending a JSON to it and it was forwarding it to the server. That server replied well to the same JSON if sent directly. The thing was that the client, in the deserialization and serialization, was sending the final reconstructed JSON with a missing field, and that made the server fail to reply. As dumb as that. The risk of assuming things.

Ángel Fas
  • 353
  • 1
  • 3
  • 15
-1

First, I would recommend not to try to instantiate an instance of WebsocketClientEndpoint() as the only implementation of the class I can find uses static connect() methods, which require either an existing instance of WebSocketClient() or WebSocketContainer() - See this example. Instead, I would recommend creating a class that extends WebSocketClient and work with that instead - see an implementation of that here.

Also, another thing that might cause a different sort of problem is the possibility of an Unhandled Exception causing code execution to prematurely abort. The URI class throws a URISyntaxException, and if you are not wrapping your new URI object instantiation in a try/catch block or denoting the current scope method/class as throws URISyntaxException (or throws Exception to handle the other exceptions that might be thrown by WebsocketClient() as well) and have the thrown Exception(s) handled in a try/catch block in outer calling context, your code may be crashing due to that.

SPQRBob
  • 1
  • 2
  • Thanks for answering. I tried your suggestion (extending WebSocketClient ) in the link. I have a different error now: "Connection closed by us Code: 1002 Reason: Invalid status code received: 301 Status line: HTTP/1.1 301 Moved Permanently". I have two mysteries now. The send in the onOpen is never executed. Besides, I try-catch the URISyntaxException, but that is not the problem. – Ángel Fas Mar 27 '22 at 01:56
  • None of this has any bearing on the meaning of 1011. – user207421 Mar 27 '22 at 03:36