Theldus / wsServer

wsServer - a tiny WebSocket server library written in C
https://theldus.github.io/wsServer
GNU General Public License v3.0
427 stars 86 forks source link

How to close the connection at server side? #12

Closed zatarus closed 3 years ago

zatarus commented 3 years ago

Pardon me if an obvious question.. Having fd on hand, is there a way to manually close connection before client hits 'disconnect' ? Thank you

Theldus commented 3 years ago

Hello @zatarus, that's a good question, At this point, wsServer has no routine that arbitrarily closes the client connection. A quick fix that you may have guessed is just call the close() function passing the fd client, but this is not the correct way to do it.

The proper way would be for the server to send a close frame to the client and then initiate a closing handshake and only then terminate the connection; maybe I should work on it, since it's something really useful to have.

zatarus commented 3 years ago

Hey @Theldus, that's good to know. I will probably signal the client with a 'close' message as a workaround (so it can be triggered from js side), until if any api becomes available. Cheers

Theldus commented 3 years ago

Hey @zatarus, I've made a PR (#13) that adds the routine ws_close_clientwith the following signature:

int ws_close_client(int fd);

that receives a client fdand returns 0 if success, -1 otherwise. I hope this routine meets your expectations, ;-).

zatarus commented 3 years ago

Thank you @Theldus, I will be switching over to this function instead of telling clients to disconnect. Cheers!