Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

websocket write problem

when I use websocket, I found that the data can't write out right away. In the test, I start a timer(10Hz) to call mg_send_websocket_frame to send binary data, but the explorer can't receive the data in time. Is there any way to send the binary data at once?

Here is my code:


#define TIMER_TM 0.1 static sig_atomic_t s_signal_received = 0; static const char *s_http_port = "9191"; static struct mg_serve_http_opts s_http_server_opts; static void signal_handler(int sig_num) { signal(sig_num, signal_handler); // Reinstantiate signal handler s_signal_received = sig_num; } static int is_websocket(const struct mg_connection *nc) { return nc->flags & MG_F_IS_WEBSOCKET; } #define test_len 2244 short test_data[test_len]; static void broadcast(struct mg_connection *nc, const char *msg, size_t len) { struct mg_connection *c; //char buf[500]; int conn_num=0; //snprintf(buf, sizeof(buf), "%p %.*s", nc, (int) len, msg); for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) { //mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, strlen(buf)); if (c->flags & MG_F_IS_WEBSOCKET) { test_data[0] = (short)(rand()%300+1); mg_send_websocket_frame(c, WEBSOCKET_OP_BINARY, (char *)test_data, test_len*sizeof(short)); conn_num++; } } printf("ws_broadcast conn num: %d\n", conn_num); } static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; struct websocket_message *wm = (struct websocket_message *) ev_data; switch (ev) { case MG_EV_HTTP_REQUEST: /* Usual HTTP request - serve static files */ mg_serve_http(nc, hm, s_http_server_opts); //nc->flags |= MG_F_SEND_AND_CLOSE; break; case MG_EV_WEBSOCKET_HANDSHAKE_DONE: /* New websocket connection. Tell everybody. */ //broadcast(nc, "joined", 6); break; case MG_EV_WEBSOCKET_FRAME: /* New websocket message. Tell everybody. */ //broadcast(nc, (char *) wm->data, wm->size); break; case MG_EV_TIMER: broadcast(nc, "test", 4); mg_set_timer(nc, mg_time() + TIMER_TM); break; case MG_EV_CLOSE: /* Disconnect. Tell everybody. */ if (is_websocket(nc)) { broadcast(nc, "left", 4); } break; default: break; } } int main(void) { struct mg_mgr mgr; struct mg_connection *nc; signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); mg_mgr_init(&mgr, NULL); nc = mg_bind(&mgr, s_http_port, ev_handler); s_http_server_opts.document_root = "www"; mg_set_protocol_http_websocket(nc); mg_set_timer(nc, mg_time() + TIMER_TM); printf("Started on port %s\n", s_http_port); while (s_signal_received == 0) { mg_mgr_poll(&mgr, 2000); } mg_mgr_free(&mgr); return 0; }

Is there any wrong?  anyone help me?

I just send data to the exporer in time when I call mg_send_websocket_frame.


Comments

  • SergeySergey Dublin, Ireland
    What do you see on the explorer side?
  • iautheriauther cd
    edited May 2016

    @Sergey Lyubka said:
    What do you see on the explorer side?

    on the explorer, the data isn't received the data immediately each time.

    through wireshark, I got the same result, which means that the data wasn't real-time send out.

    when I traced the data flow, found that it didn't run the right way.

  • adminadmin Dublin, Ireland

    Ok.
    Just to clarify, what is the platform on the server side, and what Mongoose version are you using?

  • The server side is linux on arm. Mongoose version is 6.4, the latest version.

    Now the application need a time-precise sending and receiving. I use the timer to simulate it, but the data isn't real-time transferred. I ever wrote a funtion directly used the socket, but failed because some flags in the mongoose_poll is unset.

  • adminadmin Dublin, Ireland

    Ok, I've created a project on c9 with your code - could you clarify whether it works properly with your client, please?

    https://ide.c9.io/valenok/ws1

  • Hi,

    I also have same problem with Mongoose 6.9.
    When I send string message to server it works;

    const char* buf = msg.c_str();
    mg_send_websocket_frame(this->wsCon, WEBSOCKET_OP_TEXT, buf, strlen(buf));

    But when I try to send binary frames to server, there is a delay

    mg_send_websocket_frame(this->wsCon, WEBSOCKET_OP_BINARY, data, len);

    Thanks

Sign In or Register to comment.