Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket incoming messages empty #45

Closed
mmardinian opened this issue Mar 16, 2018 · 15 comments
Closed

websocket incoming messages empty #45

mmardinian opened this issue Mar 16, 2018 · 15 comments
Labels
bug can't reproduce Can't reproduce the issue

Comments

@mmardinian
Copy link

mmardinian commented Mar 16, 2018

Hi Nicolas,

In the websocket example, I always have text payload empty.
The message information however shows that some data have been received (data length isn't null).
But the data itself seems empty.

Please let me know need further information.

Thank you.
Michael

@babelouest
Copy link
Owner

I'd like more informations please, starting with the system you're using (processor, OS, version), and more information on the problem itself. what text payload are you talking about? The one on the browser? the one on the websocket_example console? somewhere else?
what do you mean when you say "some data have been received"?

Thanks in advance for the precisions

@mmardinian
Copy link
Author

I work on an advanced replica of a raspberry Pi:
Architecture: armv7l
Byte Order: Little Endian
CPU(s): 4
Model name: ARMv7 Processor Rev 4 (v7l)
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 8.0 (jessie)
Release: 8.0
codename: jessie

The issue happens when you try to send a message from the browser interface (index.html) to the websocket. I had to modify first the example so the websocket doesn't close after sending its 5 messages to the client. So I try to send a simple word "test" writing it in the browser text box and then clicking on the send button.
I see in the console logs that the programs entry the websocket_incoming_message_callback function.
I get the following info about the message: opcode: 1, has_mask: 1, data_len: 4 (that match the word size of what I'm trying to send).
But, the message data is empty: text payload ''

@babelouest
Copy link
Owner

In websocket_manager_callback, when the 5 iterations are complete the function returns, so the websocket is closed by Ulfius: https://github.com/babelouest/ulfius/blob/master/API.md#starting-a-websocket-communication

When you say you had to modify the example so the websocket doesn't close after sending its 5 messages to the client, what is the nature of your modification?
Because if the function websocket_manager_callback ends, the behaviour may be undefined.
To avoid automatic websocket close by the server, you can either change the loop into for (i=0;; i++) { or add a getchar(); at the end of the loop to pause the program.

@mmardinian
Copy link
Author

Yes, that's what I did.
Actually more dirty: while(1) {...

@babelouest
Copy link
Owner

can you post your updated websocket_manager_callback? I'll try to reproduce your bug.

@mmardinian
Copy link
Author

There is just the "while" modification.

`void websocket_manager_callback(const struct _u_request * request,
struct _websocket_manager * websocket_manager,
void * websocket_manager_user_data) {
int i, ret;
char * my_message = (char *)websocket_manager_user_data;
if (my_message != NULL) {
y_log_message(Y_LOG_LEVEL_DEBUG, "websocket_manager_user_data is %s", my_message);
}
//for (i=0; i<5; i++) {
while(1) {
i++;
i=i%256;
sleep(2);
if (websocket_manager != NULL && websocket_manager->connected) {
my_message = msprintf("Send message #%d", i);
ret = ulfius_websocket_send_message(websocket_manager, U_WEBSOCKET_OPCODE_TEXT, o_strlen(my_message), my_message);
o_free(my_message);
if (ret != U_OK) {
y_log_message(Y_LOG_LEVEL_DEBUG, "Error send message");
break;
}

  if (i == 2) {
    ret = ulfius_websocket_send_message(websocket_manager, U_WEBSOCKET_OPCODE_PING, 0, NULL);
    if (ret != U_OK) {
      y_log_message(Y_LOG_LEVEL_DEBUG, "Error send ping message");
      break;
    }
    sleep(1);
  }
} else {
  y_log_message(Y_LOG_LEVEL_DEBUG, "websocket not connected");
  break;
}

}
y_log_message(Y_LOG_LEVEL_DEBUG, "Closing websocket_manager_callback");
}
But why do you think the issue comes from thewebsocket_manager_callback? Should it be in the websocket_incoming_message_callback`?

@babelouest
Copy link
Owner

I'm not sure actually where it comes from, I try to get all the information I need to reproduce the bug, and the only change you told me is in the websocket_incoming_message_callback function.

@babelouest
Copy link
Owner

So far I can't reproduce this bug, I transformed the for loop into an infinite loop but I still receive the messages in the websocket_incoming_message_callback function.

@babelouest
Copy link
Owner

What doo you mean when you say "advanced replica of a raspberry Pi"? Is it a physical machine or a VM? Which one?
Also, what compiler are you using? How to you build Ulfius to run the tests?

@babelouest babelouest added bug can't reproduce Can't reproduce the issue labels Mar 17, 2018
@mmardinian
Copy link
Author

Actually it's a bit more complicated.
It's an ARMv7 running Raspbian. But you application is running in a docker container.
Fun project :-)
I am using the standard GCC compiler for ARM.
Here is a screenshot of websocket logs.
You can see that the incoming message (test payload)
is not displayed....

ulfius_websocket_incomingmessageissue

@mmardinian
Copy link
Author

I've been into the u_websocket.c file in order to track the issue.
I added some logs messages, but they don't show up when I run the test.
What do I have to do in order to access such logs?

@babelouest
Copy link
Owner

The logs are provided by yder, a sublibrary, basically, yder builds a string with sprintf and puts it in the console, a file or syslog. You can replace it with a simple printf with the additional \n at the end.
The code that logs the incoming message is the following:

y_log_message(Y_LOG_LEVEL_DEBUG, "text payload '%.*s'", last_message->data_len, last_message->data);

You can try the following lines instead to check where the problem would be.

// Check if the problems comes from yder by not using it
printf("text payload '%.*s'\n", last_message->data_len, last_message->data);

// Check if the problem comes from the `%.*s` format (not likely but worth a shot)
printf("text payload at %p '%s'\n", last_message->data, last_message->data);

// Finally, print out what's inside the first bytes of last_message->data
// Careful with this last one, the incoming message must be at least 3 characters long
printf("text payload %02x %02x %02x %02x\n", last_message->data[0], last_message->data[1], last_message->data[2], last_message->data[3]);

If you want to debug directly the websocket incoming message manager, check the function ulfius_read_incoming_message. More precisely, the payload is set starting line 291 if I recall correctly: https://github.com/babelouest/ulfius/blob/fix-websockets/src/u_websocket.c#L291

@mmardinian
Copy link
Author

ok so I put some logs in the u_websocket.c and the data are well displayed in the logs.
And it turns out that I have issue when I let:
y_log_message(Y_LOG_LEVEL_DEBUG, "text payload '%.*s'", last_message->data_len, last_message->data);
But the data are well displayed when I change it into:
y_log_message(Y_LOG_LEVEL_DEBUG, "text payload '%s'", last_message->data);

@babelouest
Copy link
Owner

OK, that's weird but at least we found out where the problem is. There is no problem in the data itself, just when displaying it with a limit number of chars.

Can we consider this issue closed?

@mmardinian
Copy link
Author

Yes.
Thanks again for your support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug can't reproduce Can't reproduce the issue
Projects
None yet
Development

No branches or pull requests

2 participants