babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

Openning server websocket without help of REST Endpoint? #159

Closed alexe100 closed 4 years ago

alexe100 commented 4 years ago

Hello, me again... In the websocket_server sample it seems that websocket is opened because there is a rest endpoint that points to the main callback_websocket function, right?

Documentation refers function ulfius_start_websocket_cb to be used to start server side websockets but then there is not more details about this function e.g. syntax. Instead ulfius_set_websocket_response is presented next. Is this a documentation bug or is it like that?

My doubt here is how to create and open server side websocket without relying in rest api (a server that serves only websocket and zero rest api)?

Sorry about all these posts :-( but your framework seems very very interesting and flexible when compared with similar ones.

Alex

babelouest commented 4 years ago

Hello,

A websocket service is a http service first, the websocket part is kind of an extension of the http connection. Therefore to connect to a websocket, you need to know its http address first. That's why on ulfius, you can start a websocket server only inside an endpoint callback. Maybe the documentation isn't clear about that. If you want the websocket to be available for all addresses of your webserver, you can create a unique endpoint and open it for all addresses:

ulfius_add_endpoint_by_val(&instance, "GET", NULL, "*", 0, &callback_function, NULL);

Note that a websocket must be accessed using a GET method.

alexe100 commented 4 years ago

Got it! Thank you