New example - Public network access

This commit is contained in:
Hassan DRAGA 2023-11-29 18:22:35 -05:00
parent ff6fdb1361
commit c063f07dcd
3 changed files with 327 additions and 0 deletions

View File

@ -0,0 +1,141 @@
# WebUI C Example
# == 1. VARIABLES =============================================================
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_DIR := $(dir $(MAKEFILE_PATH))/../../../
TARGET := $(firstword $(MAKECMDGOALS))
LIB_DIR := $(PROJECT_DIR)/dist
ifeq ($(TARGET), debug)
LIB_DIR := $(LIB_DIR)/debug
endif
INCLUDE_DIR := $(PROJECT_DIR)/include
WEBUI_LIB_NAME = webui-2
ifeq ($(WEBUI_USE_TLS), 1)
WEBUI_LIB_NAME = webui-2-secure
endif
# ARGS
# Set a compiler when running on Linux via `make CC=gcc` / `make CC=clang`
CC = gcc
# Build the WebUI library if running via `make BUILD_LIB=true`
BUILD_LIB ?=
# BUILD FLAGS
STATIC_BUILD_FLAGS = main.c -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)"
DYN_BUILD_FLAGS = main.c -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)"
# Platform conditions
ifeq ($(OS),Windows_NT)
# Windows
PLATFORM := windows
SHELL := CMD
STATIC_BUILD_FLAGS += -l$(WEBUI_LIB_NAME)-static -lws2_32 -Wall -luser32 -static
COPY_LIB_CMD := @copy "$(LIB_DIR)\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll"
DYN_BUILD_FLAGS += "$(WEBUI_LIB_NAME).dll" -lws2_32 -Wall -luser32
STATIC_OUT := main.exe
DYN_OUT := main-dyn.exe
LWS2_OPT := -lws2_32
STRIP_OPT := --strip-all
CONSOLE_APP := -Wl,-subsystem=console
GUI_APP := -Wl,-subsystem=windows
else
STATIC_BUILD_FLAGS += -lpthread -lm -l$(WEBUI_LIB_NAME)-static
DYN_BUILD_FLAGS += -lpthread -lm
STATIC_OUT := main
DYN_OUT := main-dyn
CONSOLE_APP :=
GUI_APP :=
ifeq ($(shell uname),Darwin)
# MacOS
PLATFORM := macos
CC = clang
COPY_LIB_CMD := @cp "$(LIB_DIR)/$(WEBUI_LIB_NAME).dylib" "$(WEBUI_LIB_NAME).dylib"
DYN_BUILD_FLAGS += "./$(WEBUI_LIB_NAME).dylib"
else
# Linux
PLATFORM := linux
COPY_LIB_CMD := @cp "$(LIB_DIR)/$(WEBUI_LIB_NAME).so" "$(WEBUI_LIB_NAME).so"
DYN_BUILD_FLAGS += "./$(WEBUI_LIB_NAME).so"
STRIP_OPT := --strip-all
ifeq ($(CC),clang)
LLVM_OPT := llvm-
endif
endif
endif
# == 2.TARGETS ================================================================
all: release
debug: --validate-args
ifeq ($(BUILD_LIB),true)
@cd "$(PROJECT_DIR)" && $(MAKE) debug
endif
# Static with Debug info
ifneq ($(WEBUI_USE_TLS), 1)
@echo "Build C Example ($(CC) debug static)..."
@$(CC) -g $(CONSOLE_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) -o $(STATIC_OUT)
endif
# Dynamic with Debug info
@echo "Build C Example ($(CC) debug dynamic)..."
$(COPY_LIB_CMD)
@$(CC) -g $(CONSOLE_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) -o $(DYN_OUT)
# Clean
ifeq ($(PLATFORM),windows)
@- del *.o >nul 2>&1
else
@- rm -f *.o
@- rm -rf *.dSYM # macOS
endif
@echo "Done."
release: --validate-args
ifeq ($(BUILD_LIB),true)
@cd "$(PROJECT_DIR)" && $(MAKE)
endif
# Static Release
ifneq ($(WEBUI_USE_TLS), 1)
@echo "Build C Example ($(CC) release static)..."
@$(CC) -Os $(GUI_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) -o $(STATIC_OUT)
@$(LLVM_OPT)strip $(STRIP_OPT) $(STATIC_OUT)
endif
# Dynamic Release
@echo "Build C Example ($(CC) release dynamic)..."
$(COPY_LIB_CMD)
@$(CC) $(GUI_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) -o $(DYN_OUT)
@$(LLVM_OPT)strip $(STRIP_OPT) $(DYN_OUT)
# Clean
ifeq ($(PLATFORM),windows)
@- del *.o >nul 2>&1
else
@- rm -f *.o
@- rm -rf *.dSYM # macOS
endif
@echo "Done."
clean: --clean-$(PLATFORM)
# INTERNAL TARGETS
--validate-args:
ifneq ($(filter $(CC),gcc clang aarch64-linux-gnu-gcc arm-linux-gnueabihf-gcc musl-gcc),$(CC))
$(error Invalid compiler specified: `$(CC)`)
endif
--clean-linux: --clean-unix
--clean-macos: --clean-unix
--clean-unix:
- rm -f *.o
- rm -f *.a
- rm -f *.so
- rm -f *.dylib
- rm -rf *.dSYM
--clean-windows:
- del *.o >nul 2>&1
- del *.dll >nul 2>&1
- del *.a >nul 2>&1
- del *.exe >nul 2>&1

View File

@ -0,0 +1,65 @@
# WebUI C Example
# Windows - Microsoft Visual C
SHELL = CMD
LIB_DIR = ../../../dist
INCLUDE_DIR = ../../../include
WEBUI_LIB_NAME = webui-2
!IF "$(WEBUI_USE_TLS)" == "1"
WEBUI_LIB_NAME = webui-2-secure
!ENDIF
# Build the WebUI library if running `nmake BUILD_LIB=true`
BUILD_LIB =
all: release
debug:
!IF "$(BUILD_LIB)" == "true"
@cd "$(LIB_DIR)" && cd .. && $(MAKE) debug
!ENDIF
# Static with Debug info
!IF "$(WEBUI_USE_TLS)" != "1"
@echo Build C Example (Static Debug)...
@cl /Zi main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)\debug" /MACHINE:X64 /SUBSYSTEM:CONSOLE $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib /OUT:main.exe 1>NUL 2>&1
!ENDIF
# Dynamic with Debug info
@echo Build C Example (Dynamic Debug)...
@copy "$(LIB_DIR)\debug\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll"
@cl /Zi main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)\debug" /MACHINE:X64 /SUBSYSTEM:CONSOLE $(WEBUI_LIB_NAME).lib user32.lib Advapi32.lib Shell32.lib /OUT:main-dyn.exe 1>NUL 2>&1
# Clean
@- del *.exp >nul 2>&1
@- del *.ilk >nul 2>&1
@- del *.lib >nul 2>&1
@- del *.obj >nul 2>&1
@- del *.pdb >nul 2>&1
@echo Done.
release:
!IF "$(BUILD_LIB)" == "true"
@cd "$(LIB_DIR)" && cd .. && $(MAKE)
!ENDIF
# Static Release
!IF "$(WEBUI_USE_TLS)" != "1"
@echo Build C Example (Static Release)...
@cl main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)" /MACHINE:X64 /SUBSYSTEM:WINDOWS $(WEBUI_LIB_NAME)-static.lib user32.lib Advapi32.lib Shell32.lib /OUT:main.exe 1>NUL 2>&1
!ENDIF
# Dynamic Release
@echo Build C Example (Dynamic Release)...
@copy "$(LIB_DIR)\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll"
@cl main.c /I"$(INCLUDE_DIR)" /link /LIBPATH:"$(LIB_DIR)" /MACHINE:X64 /SUBSYSTEM:WINDOWS $(WEBUI_LIB_NAME).lib user32.lib Advapi32.lib Shell32.lib /OUT:main-dyn.exe 1>NUL 2>&1
# Clean
@- del *.exp >nul 2>&1
@- del *.ilk >nul 2>&1
@- del *.lib >nul 2>&1
@- del *.obj >nul 2>&1
@- del *.pdb >nul 2>&1
@echo Done.
clean:
- del *.obj >nul 2>&1
- del *.ilk >nul 2>&1
- del *.pdb >nul 2>&1
- del *.exp >nul 2>&1
- del *.exe >nul 2>&1
- del *.lib >nul 2>&1

View File

@ -0,0 +1,121 @@
// Public Network Access Example
#include "webui.h"
// Windows
size_t private_window = 0;
size_t public_window = 0;
void app_exit(webui_event_t* e) {
webui_exit();
}
void public_window_events(webui_event_t* e) {
if (e->event_type == WEBUI_EVENT_CONNECTED) {
// New connection
webui_run(private_window, "document.getElementById(\"Logs\").value += \"New connection.\\n\";");
}
else if (e->event_type == WEBUI_EVENT_DISCONNECTED) {
// Disconnection
webui_run(private_window, "document.getElementById(\"Logs\").value += \"Disconnected.\\n\";");
}
}
int main() {
// Main Private Window HTML
const char* private_html = "<!DOCTYPE html>"
"<html>"
" <head>"
" <meta charset=\"UTF-8\">"
" <script src=\"webui.js\"></script>"
" <title>Public Network Access Example</title>"
" <style>"
" body {"
" font-family: 'Arial', sans-serif;"
" color: white;"
" background: linear-gradient(to right, #507d91, #1c596f, #022737);"
" text-align: center;"
" font-size: 18px;"
" }"
" button, input {"
" padding: 10px;"
" margin: 10px;"
" border-radius: 3px;"
" border: 1px solid #ccc;"
" box-shadow: 0 3px 5px rgba(0,0,0,0.1);"
" transition: 0.2s;"
" }"
" button {"
" background: #3498db;"
" color: #fff; "
" cursor: pointer;"
" font-size: 16px;"
" }"
" h1 { text-shadow: -7px 10px 7px rgb(67 57 57 / 76%); }"
" button:hover { background: #c9913d; }"
" input:focus { outline: none; border-color: #3498db; }"
" </style>"
" </head>"
" <body>"
" <h1>WebUI - Public Network Access Example</h1>"
" <br>"
" The second public window is configured to be accessible from <br>"
" any device in the public network. <br>"
" <br>"
" Second public window link: <br>"
" <h1 id=\"urlSpan\" style=\"color:#c9913d\">...</h1>"
" Second public window events: <br>"
" <textarea id=\"Logs\" rows=\"4\" cols=\"50\" style=\"width:80%\"></textarea>"
" <br>"
" <button id=\"Exit\">Exit</button>"
" </body>"
"</html>";
// Public Window HTML
const char* public_html = "<!DOCTYPE html>"
"<html>"
" <head>"
" <meta charset=\"UTF-8\">"
" <script src=\"webui.js\"></script>"
" <title>Welcome to Public UI</title>"
" </head>"
" <body>"
" <h1>Welcome to Public UI!</h1>"
" </body>"
"</html>";
// Create windows
private_window = webui_new_window();
public_window = webui_new_window();
// App
webui_set_timeout(0); // Wait forever (never timeout)
// Public Window
webui_set_public(public_window, true); // Make URL accessible from public networks
webui_bind(public_window, "", public_window_events); // Bind all events
webui_show_browser(public_window, public_html, NoBrowser); // Set public window HTML
const char* public_win_url = webui_get_url(public_window); // Get URL of public window
// Main Private Window
webui_bind(private_window, "Exit", app_exit); // Bind exit button
webui_show(private_window, private_html); // Show the window
// Set URL in the UI
char javascript[1024];
sprintf(javascript, "document.getElementById('urlSpan').innerHTML = '%s';", public_win_url);
webui_run(private_window, javascript);
// Wait until all windows get closed
webui_wait();
// Free all memory resources (Optional)
webui_clean();
return 0;
}
#if defined(_MSC_VER)
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) { return main(); }
#endif