Handling WebSocket Reconnection with AWS API Gateway and Protocol Persistence in Codename One

77 views Asked by At

Codename One Community,

I'm currently working on a project that involves using the Codename One WebSocket class in conjunction with AWS API Gateway. AWS API Gateway allows sending back the Sec-WebSocket-Protocol field as part of the response from the $connect route, enabling clients to request specific subprotocols while connecting to the WebSocket API.

I've encountered a challenge with the Codename One WebSocket class's reconnection (AutoReconnect) feature. Specifically, when setting a protocol through the WebSocket class, this protocol information is lost upon reconnection. This is crucial for my use case, as I send authorization and user information through the protocol, which is then lost during reconnection.

To address this, I created my own version of the WebSocket class, adding a private String variable to store the protocol and modifying the reconnect function to set the protocol back during the reconnection process. This solution works fine for connecting, reconnecting, and disconnection. However, the OnOpen, OnMessage, OnClose, and OnError methods do not work as expected. I suspect this is due to the native functionality not being able to handle this external modification.

As a workaround, I stopped using the reconnect feature of the Codename One WebSocket class and instead manually triggered the creation of a new WebSocket object in the OnClose method, effectively creating a recursive reconnection mechanism. This approach seems to work well.

My question to the Codename One team and the community is:

What are your thoughts on my approach? Do you see any potential issues or have suggestions for improvement?

Is there a plan to enhance the Codename One WebSocket class to better handle protocol persistence in the reconnection process? Additionally, could there be a possibility to add header information for security purposes in future updates? Any insights or advice on this matter would be greatly appreciated.

Thank you for your time and assistance.

Oscar Naranjo

1

There are 1 answers

1
Shai Almog On

The reconnect code is relatively simple and implemented in Java space as you can see here. It's hard to tell without your code but if I understand you correctly it might be best to fork the project, add an enhancement to include the meta-data you need and submit a pull request.

It might make sense to open an issue on the project first so you can discuss the generic approach for solving that specific problem.