From 640d9d97c64c22d9a33195364572a11660d4909f Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 6 Apr 2021 18:02:45 +0200 Subject: [PATCH] Broadacst unix time in 8ms intervals, example --- Makefile | 2 +- examples/Broadcast.cpp | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 4197c90..b4606b7 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -EXAMPLE_FILES := HelloWorld ServerName EchoServer BroadcastingEchoServer UpgradeSync UpgradeAsync +EXAMPLE_FILES := Broadcast HelloWorld ServerName EchoServer BroadcastingEchoServer UpgradeSync UpgradeAsync THREADED_EXAMPLE_FILES := HelloWorldThreaded EchoServerThreaded override CXXFLAGS += -lpthread -Wpedantic -Wall -Wextra -Wsign-conversion -Wconversion -std=c++2a -Isrc -IuSockets/src override LDFLAGS += uSockets/*.o -lz diff --git a/examples/Broadcast.cpp b/examples/Broadcast.cpp index bdf6e1e..4908cb6 100644 --- a/examples/Broadcast.cpp +++ b/examples/Broadcast.cpp @@ -1,5 +1,7 @@ /* We simply call the root header file "App.h", giving you uWS::App and uWS::SSLApp */ #include "App.h" +#include +#include /* 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;