Broadacst unix time in 8ms intervals, example
This commit is contained in:
@@ -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
|
||||
|
||||
+14
-4
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user