Broadacst unix time in 8ms intervals, example

This commit is contained in:
Alex Hultman
2021-04-06 18:02:45 +02:00
parent 42d480e529
commit 640d9d97c6
2 changed files with 15 additions and 5 deletions
+14 -4
View File
@@ -1,5 +1,7 @@
/* We simply call the root header file "App.h", giving you uWS::App and uWS::SSLApp */
#include "App.h"
#include <time.h>
#include <iostream>
/* This is a simple WebSocket echo server example.
* You may compile it with "WITH_OPENSSL=1 make" or with "make" */
@@ -34,7 +36,7 @@ int main() {
/* Open event here, you may access ws->getUserData() which points to a PerSocketData struct */
ws->subscribe("broadcast");
},
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
.message = [](auto */*ws*/, std::string_view /*message*/, uWS::OpCode /*opCode*/) {
},
.drain = [](auto */*ws*/) {
@@ -58,11 +60,19 @@ int main() {
struct us_loop_t *loop = (struct us_loop_t *) uWS::Loop::get();
struct us_timer_t *delayTimer = us_create_timer(loop, 0, 0);
us_timer_set(delayTimer, [](struct us_timer_t *t) {
// broadcast the unix time as millis every 8 millis
us_timer_set(delayTimer, [](struct us_timer_t */*t*/) {
globalApp->publish("broadcast", "1234567891234", uWS::OpCode::TEXT, false);
struct timespec ts;
timespec_get(&ts, TIME_UTC);
}, 75, 75);
int64_t millis = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
//std::cout << "Broadcasting timestamp: " << millis << std::endl;
globalApp->publish("broadcast", std::string_view((char *) &millis, sizeof(millis)), uWS::OpCode::BINARY, false);
}, 8, 8);
globalApp = &app;