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.08k stars 182 forks source link

[Issue] Improve Sending data over websocket #139

Closed mickae1 closed 5 years ago

mickae1 commented 5 years ago

Hi,

When I want to send data over websocket in a while loop, I've to do something like that:

while(1){
....
iError = ulfius_websocket_wait_close(pSession->websocket_manager, 100);
if ( iError == U_WEBSOCKET_STATUS_OPEN) {
    printf("\t session %s send data 3\n", pSession->acToken);
    ulfius_websocket_send_message(pSession->websocket_manager, U_WEBSOCKET_OPCODE_TEXT, iSize, acMessage);
    printf("\t session %s send data 4\n", pSession->acToken);
}else{
    iError = ulfius_websocket_wait_close(pSession->websocket_manager, 1000);
    if ( iError == U_WEBSOCKET_STATUS_OPEN) {
        printf("\t session %s send data 5\n", pSession->acToken);
        ulfius_websocket_send_message(pSession->websocket_manager, U_WEBSOCKET_OPCODE_TEXT, iSize, acMessage);
        printf("\t session %s send data 6\n", pSession->acToken);
    }else{
        printf("\terror %d wait close for %s\n", iError, pSession->acToken);
    }
}
}

Why do I need to use the function ulfius_websocket_wait_close ..... And sometime if the timeout is too short, i've to repeat the procedure to make it work.

I don't like to use ulfius_websocket_wait_close , because if you put a 2s timeout, it will wait 2s.

ulfius 2.6.3

babelouest commented 5 years ago

Hello,

You don't have to use ulfius_websocket_wait_close if you don't want to. This function is part of the Websocket status functions explained in the documentation.

The function ulfius_websocket_wait_close returns the value U_WEBSOCKET_STATUS_OPEN if the websocket is still open at the end of the specified timeout and U_WEBSOCKET_STATUS_CLOSE is the websocket is closed.

If you want to get the websocket status without timeout, you can simply use ulfius_websocket_status, this will return the same values than ulfius_websocket_wait_close. The intention of these functions is to avoid sending messages on a websocket that has been closed by the client or by the network.

In the websocket example program, the server is intended to send a message every 2 seconds.

But you can definitely use another method to manage your messages sent to the websocket, that's up to your implementation.

babelouest commented 5 years ago

@mickae1 , does this answer your question?

mickae1 commented 5 years ago

Yes thanks.

Le lun. 23 sept. 2019 à 18:59, Nicolas Mora notifications@github.com a écrit :

@mickae1 https://github.com/mickae1 , does this answer your question?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/babelouest/ulfius/issues/139?email_source=notifications&email_token=ABAHMCHHFXXICILOPZNTVZ3QLDYW5A5CNFSM4IX4DLL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7LRMUI#issuecomment-534189649, or mute the thread https://github.com/notifications/unsubscribe-auth/ABAHMCGT5ZOVXJZ6JEPHOADQLDYW5ANCNFSM4IX4DLLQ .

babelouest commented 5 years ago

Ok, I'm closing the issue then.