ws_timestamp - get WebSockets current timestamp

uint64_t ws_timestamp(struct websockets *ws)

The WebSockets event-loop concept of “now”.

Note

the timestamp is updated at the start of each event-loop iteration

Parameters
  • ws – the WebSockets handle created with ws_init()

Returns

the timestamp in milliseconds from when ws_timestamp_update() was last called

Example

uint64_t time_start, time_end;

void on_connect(void *data,
                struct websockets *ws,
                struct ws_info *info,
                const char *ws_protocols)
{
  /* get timestamp of when connection began */
  time_start = ws_timestamp(ws);
}

void on_close(void *data,
              struct websockets *ws,
              struct ws_info *info,
              enum ws_close_reason wscode,
              const char *reason,
              size_t len)
{
  /* get timestamp of when connection ended */
  time_end = ws_timestamp(ws);
}

int main(void)
{
  struct ws_callbacks cbs = { .on_connect = &on_connect,
                              .on_close = &on_close };
  CURLM *mhandle = curl_multi_init();
  struct websockets *ws = ws_init(&cbs, mhandle, NULL);
  ws_set_url(ws, "wss://example.com", NULL);

  /* run the event-loop */
  ws_start(ws);

  uint64_t tstamp;
  bool is_running;
  do {
    is_running = ws_easy_run(ws, 5, &tstamp);
  } while (is_running);

  printf("Connection lasted %ld milliseconds\n", (long)(time_end - time_start));

  ws_end(ws);

  ws_cleanup(ws);
  curl_multi_cleanup(mhandle);
}