IXWebSocket icon indicating copy to clipboard operation
IXWebSocket copied to clipboard

Use of common socket between ix::WebSocket and ix::HttpClient

Open eddie0git opened this issue 3 years ago • 4 comments

Hello. If I understood the code right (but I may be wrong), in case you have to do a few HTTP/HTTPS connections (for example for auth purposes) before finally upgrading from HTTP to WebSocket, there is no way you can use only one TCP socket but you have to use two separate sockets (one for plain http client and one for websocket). Is there a specific reason for this design choice? Could it be an improvement to declare an ix::WebSocket and share its socket when building the ix:HttpClient (for example by using a crafted additional constructor)? Thanks for the clarification Regards

eddie0git avatar Feb 02 '23 15:02 eddie0git

It feels like it would be possible, but it would increase the API surface which can be a blessing and a curse. I'm not sure exposing the backing socket is a good idea, I can see how it could lead to bugs or weird crashes if a stale pointer is used too late.

The auth can be implemented in the ws protocol, I've seen that a few time with chat protocols.

bsergean avatar Feb 10 '23 20:02 bsergean

Not sure what @eddir0git is saying. There are two kinds of sockets: a server listen socket and a client communication socket. AFAICT when you use HttpServer, you're subclassing WebSocketServer and they both listen on a single socket. A client request opens a new client socket. Once that connection is upgraded to WebSocket protocol, it cannot go back to HTTP. However, new client requests can be made and use the HTTP protocol in parallel.

fweiss avatar Feb 26 '23 21:02 fweiss

I am sorry if I have not been clear enough. I am working on the client side (hence I was talking about ix::HttpClient) and I have no control on what authorization mechanism the server developers decide to have. In order to get my authorization I have to talk HTTP(S) to the server then ask my HTTP upgrade. The only way to do that with the library (if I have understood the code right) seems to be opening two sockets and duplicating everything in HTTP (socket plus headers, SSL certificates, etc) till you are upgraded to WS (of course you dont go back to HTTP after that!). I found it a bit strange you cannot have a single TCP socket where you can talk HTTP till you are upgraded. Maye I am wrong. I suppose you can live with that (the OS will probably use a single socket if you close the HTTP client before opening the WS) but I do not find it ideally designed. The library is still very well written and many thanks to Benjamin for the work he is sharing.

eddie0git avatar Feb 27 '23 08:02 eddie0git

I’m not sure this is feasible currently, there’s a python library (websockets) which I’ve been using which support this, at least for the server side (like you can return an http response from a web socket server).

Someone wrote a PR which I merged to have the web socket and an http server run on the same port, but that’s different.

This library try to have a simple API, relatively close to the web-browser apis, which is limiting, but user friendly for the 95% use case. There are other C libraries which you could research which might be more flexible and support this flow (to only use one TCP socket), libwebsocket comes to mind, and also libcurl has new experimental support for web socket, so you could check this one too.

On Feb 27, 2023, at 12:43 AM, eddie0git @.***> wrote:

I am sorry if I have not been clear enough. I am working on the client side (hence I was talking about ix::HttpClient) and I have no control on what authorization mechanism the server developers decide to have. In order to get my authorization I have to talk HTTP(S) to the server then ask my HTTP upgrade. The only way to do that with the library (if I have understood the code right) seems to be opening two sockets and duplicating everything in HTTP (socket plus headers, SSL certificates, etc) till you are upgraded to WS (of course you dont go back to HTTP after that!). I found it a bit strange you cannot have a single TCP socket where you can talk HTTP till you are upgraded. Maye I am wrong. I suppose you can live with that (the OS will probably use a single socket if you close the HTTP client before opening the WS) but I do not find it ideally designed. The library is still very well written and many thanks to Benjamin for the work he is sharing.

— Reply to this email directly, view it on GitHub https://github.com/machinezone/IXWebSocket/issues/434#issuecomment-1445921014, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC2O6UNJBC3IZPJOW3LZSHDWZRSKDANCNFSM6AAAAAAUPFG5D4. You are receiving this because you commented.

bsergean avatar Feb 27 '23 16:02 bsergean