summaryrefslogtreecommitdiffstats
path: root/src/civetweb/examples/_obsolete/websocket
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/civetweb/examples/_obsolete/websocket
parentInitial commit. (diff)
downloadceph-upstream/16.2.11+ds.tar.xz
ceph-upstream/16.2.11+ds.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/civetweb/examples/_obsolete/websocket')
-rw-r--r--src/civetweb/examples/_obsolete/websocket/Makefile36
-rw-r--r--src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c225
-rw-r--r--src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h44
-rw-r--r--src/civetweb/examples/_obsolete/websocket/websock.htm55
-rw-r--r--src/civetweb/examples/_obsolete/websocket/websocket.c65
5 files changed, 425 insertions, 0 deletions
diff --git a/src/civetweb/examples/_obsolete/websocket/Makefile b/src/civetweb/examples/_obsolete/websocket/Makefile
new file mode 100644
index 000000000..3d6549297
--- /dev/null
+++ b/src/civetweb/examples/_obsolete/websocket/Makefile
@@ -0,0 +1,36 @@
+#
+# Copyright (c) 2013 No Face Press, LLC
+# License http://opensource.org/licenses/mit-license.php MIT License
+#
+
+#This makefile is used to test the other Makefiles
+
+
+PROG = websocket
+SRC = WebSockCallbacks.c websocket.c
+
+TOP = ../..
+CIVETWEB_LIB = libcivetweb.a
+
+CFLAGS = -I$(TOP)/include $(COPT)
+LIBS = -lpthread
+
+include $(TOP)/resources/Makefile.in-os
+
+ifeq ($(TARGET_OS),LINUX)
+ LIBS += -ldl
+endif
+
+all: $(PROG)
+
+$(PROG): $(CIVETWEB_LIB) $(SRC)
+ $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $(SRC) $(CIVETWEB_LIB) $(LIBS)
+
+$(CIVETWEB_LIB):
+ $(MAKE) -C $(TOP) clean lib WITH_WEBSOCKET=1
+ cp $(TOP)/$(CIVETWEB_LIB) .
+
+clean:
+ rm -f $(CIVETWEB_LIB) $(PROG)
+
+.PHONY: all clean
diff --git a/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c
new file mode 100644
index 000000000..bb81fb0cc
--- /dev/null
+++ b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.c
@@ -0,0 +1,225 @@
+/* This example uses deprecated interfaces: global websocket callbacks.
+ They have been superseeded by URI specific callbacks.
+ See examples/embedded_c for an up to date example.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include "WebSockCallbacks.h"
+
+#ifdef _WIN32
+#include <windows.h>
+#define mg_sleep(x) Sleep(x)
+#else
+#include <unistd.h>
+#include <pthread.h>
+#define mg_sleep(x) usleep((x)*1000)
+#endif
+
+
+static void
+send_to_all_websockets(struct mg_context *ctx, const char *data, int data_len)
+{
+
+ int i;
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+
+ mg_lock_context(ctx);
+ for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) {
+ if (ws_ctx->socketList[i]
+ && (ws_ctx->socketList[i]->webSockState == 2)) {
+ mg_websocket_write(ws_ctx->socketList[i]->conn,
+ WEBSOCKET_OPCODE_TEXT,
+ data,
+ data_len);
+ }
+ }
+ mg_unlock_context(ctx);
+}
+
+
+void
+websocket_ready_handler(struct mg_connection *conn, void *_ignored)
+{
+
+ int i;
+ const struct mg_request_info *rq = mg_get_request_info(conn);
+ struct mg_context *ctx = mg_get_context(conn);
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+ tWebSockInfo *wsock = malloc(sizeof(tWebSockInfo));
+ assert(wsock);
+ wsock->webSockState = 0;
+ mg_set_user_connection_data(conn, wsock);
+
+ mg_lock_context(ctx);
+ for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) {
+ if (0 == ws_ctx->socketList[i]) {
+ ws_ctx->socketList[i] = wsock;
+ wsock->conn = conn;
+ wsock->webSockState = 1;
+ break;
+ }
+ }
+ printf("\nNew websocket attached: %s:%u\n",
+ rq->remote_addr,
+ rq->remote_port);
+ mg_unlock_context(ctx);
+}
+
+
+static void
+websocket_done(tWebSockContext *ws_ctx, tWebSockInfo *wsock)
+{
+
+ int i;
+
+ if (wsock) {
+ wsock->webSockState = 99;
+ for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) {
+ if (wsock == ws_ctx->socketList[i]) {
+ ws_ctx->socketList[i] = 0;
+ break;
+ }
+ }
+ printf("\nClose websocket attached: %s:%u\n",
+ mg_get_request_info(wsock->conn)->remote_addr,
+ mg_get_request_info(wsock->conn)->remote_port);
+ free(wsock);
+ }
+}
+
+
+int
+websocket_data_handler(struct mg_connection *conn,
+ int flags,
+ char *data,
+ size_t data_len,
+ void *_ignored)
+{
+
+ const struct mg_request_info *rq = mg_get_request_info(conn);
+ tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data;
+ struct mg_context *ctx = mg_get_context(conn);
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+ char msg[128];
+
+ mg_lock_context(ctx);
+ if (flags == 136) {
+ // close websock
+ websocket_done(ws_ctx, wsock);
+ mg_set_user_connection_data(conn, NULL);
+ mg_unlock_context(ctx);
+ return 1;
+ }
+ if (((data_len >= 5) && (data_len < 100) && (flags == 129))
+ || (flags == 130)) {
+
+ // init command
+ if ((wsock->webSockState == 1) && (!memcmp(data, "init ", 5))) {
+ char *chk;
+ unsigned long gid;
+ memcpy(msg, data + 5, data_len - 5);
+ msg[data_len - 5] = 0;
+ gid = strtoul(msg, &chk, 10);
+ wsock->initId = gid;
+ if (gid > 0 && chk != NULL && *chk == 0) {
+ wsock->webSockState = 2;
+ }
+ mg_unlock_context(ctx);
+ return 1;
+ }
+
+ // chat message
+ if ((wsock->webSockState == 2) && (!memcmp(data, "msg ", 4))) {
+ send_to_all_websockets(ctx, data, data_len);
+ mg_unlock_context(ctx);
+ return 1;
+ }
+ }
+
+ // keep alive
+ if ((data_len == 4) && !memcmp(data, "ping", 4)) {
+ mg_unlock_context(ctx);
+ return 1;
+ }
+
+ mg_unlock_context(ctx);
+ return 0;
+}
+
+
+void
+connection_close_handler(const struct mg_connection *conn, void *_ignored)
+{
+
+ const struct mg_request_info *rq = mg_get_request_info(conn);
+ tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data;
+ struct mg_context *ctx = mg_get_context(conn);
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+
+ mg_lock_context(ctx);
+ websocket_done(ws_ctx, wsock);
+ mg_set_user_connection_data(conn, NULL);
+ mg_unlock_context(ctx);
+}
+
+
+static void *
+eventMain(void *arg)
+{
+
+ char msg[256];
+ struct mg_context *ctx = (struct mg_context *)arg;
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+
+ ws_ctx->runLoop = 1;
+ while (ws_ctx->runLoop) {
+ time_t t = time(0);
+ struct tm *timestr = localtime(&t);
+ strftime(msg, sizeof(msg), "title %c", timestr);
+ send_to_all_websockets(ctx, msg, strlen(msg));
+
+ mg_sleep(1000);
+ }
+
+ return NULL;
+}
+
+
+void
+websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len)
+{
+
+ char buffer[260];
+
+ if (data_len <= 256) {
+ strcpy(buffer, "msg ");
+ memcpy(buffer + 4, data, data_len);
+
+ send_to_all_websockets(ctx, buffer, data_len + 4);
+ }
+}
+
+
+void
+websock_init_lib(const struct mg_context *ctx)
+{
+
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+ memset(ws_ctx, 0, sizeof(*ws_ctx));
+ /* todo: use mg_start_thread_id instead of mg_start_thread */
+ mg_start_thread(eventMain, (void *)ctx);
+}
+
+
+void
+websock_exit_lib(const struct mg_context *ctx)
+{
+
+ tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx);
+ ws_ctx->runLoop = 0;
+ /* todo: wait for the thread instead of a timeout */
+ mg_sleep(2000);
+}
diff --git a/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h
new file mode 100644
index 000000000..f44821daf
--- /dev/null
+++ b/src/civetweb/examples/_obsolete/websocket/WebSockCallbacks.h
@@ -0,0 +1,44 @@
+
+#ifndef WEBSOCKCALLBACKS_H_INCLUDED
+#define WEBSOCKCALLBACKS_H_INCLUDED
+
+#include "civetweb.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct tWebSockInfo {
+ int webSockState;
+ unsigned long initId;
+ struct mg_connection *conn;
+} tWebSockInfo;
+
+#define MAX_NUM_OF_WEBSOCKS (256)
+typedef struct tWebSockContext {
+ int runLoop;
+ void *thread_id;
+ tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS];
+} tWebSockContext;
+
+
+void websock_init_lib(const struct mg_context *ctx);
+void websock_exit_lib(const struct mg_context *ctx);
+
+void
+websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len);
+
+void websocket_ready_handler(struct mg_connection *conn, void *_ignored);
+int websocket_data_handler(struct mg_connection *conn,
+ int flags,
+ char *data,
+ size_t data_len,
+ void *_ignored);
+void connection_close_handler(const struct mg_connection *conn, void *_ignored);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif \ No newline at end of file
diff --git a/src/civetweb/examples/_obsolete/websocket/websock.htm b/src/civetweb/examples/_obsolete/websocket/websock.htm
new file mode 100644
index 000000000..4ff3a5fae
--- /dev/null
+++ b/src/civetweb/examples/_obsolete/websocket/websock.htm
@@ -0,0 +1,55 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <title>Test</title>
+ <script type='text/javascript' language="javascript">
+ <!--
+ var connection;
+ var keepAlive = false;
+
+ function webSockKeepAlive() {
+ if (keepAlive) {
+ connection.send('ping'); // Send the message 'ping' to the server
+ setTimeout("webSockKeepAlive()", 10000);
+ }
+ }
+
+ function load() {
+ connection = new WebSocket("ws://127.0.0.1/MyWebSock");
+
+ connection.onopen = function () {
+ var send = "init " + Math.round(Math.random()*4294967294+1);
+ console.log('Client: ' + send);
+ connection.send(send);
+ keepAlive = true;
+ webSockKeepAlive();
+ };
+
+ connection.onerror = function (error) {
+ keepAlive = false;
+ connection.close();
+ console.log('WebSocket error: ' + error);
+ alert("WebSocket error");
+ };
+
+ connection.onmessage = function (e) {
+ console.log('Server: ' + e.data);
+ if (e.data.substring(0,5) == "title") {window.document.title = e.data.substring(6);}
+ else if (e.data.substring(0,3) == "msg") {
+ var msgStr = document.getElementById('msg');
+ msgStr.innerHTML = msgStr.innerHTML + e.data.substring(4);
+ }
+ };
+ }
+ //-->
+ </script>
+
+</head>
+<body onload="load()">
+ <input type="button" onclick="connection.send('msg A');" value="A"></button>
+ <input type="button" onclick="connection.send('msg B');" value="B"></button>
+ <input type="button" onclick="connection.send('msg C');" value="C"></button>
+ <input type="button" onclick="connection.send('msg D');" value="D"></button>
+ <b id="msg"></b>
+</body>
+</html> \ No newline at end of file
diff --git a/src/civetweb/examples/_obsolete/websocket/websocket.c b/src/civetweb/examples/_obsolete/websocket/websocket.c
new file mode 100644
index 000000000..3aadf98b8
--- /dev/null
+++ b/src/civetweb/examples/_obsolete/websocket/websocket.c
@@ -0,0 +1,65 @@
+/* This example uses deprecated interfaces: global websocket callbacks.
+ They have been superseeded by URI specific callbacks.
+ See examples/embedded_c for an up to date example.
+ */
+
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+
+#include "civetweb.h"
+#include "WebSockCallbacks.h"
+
+int
+main(void)
+{
+ struct mg_context *ctx = 0;
+ struct mg_callbacks callback_funcs = {0};
+ tWebSockContext ws_ctx;
+ char inbuf[4];
+
+ const char *server_options[] = {
+ /* document_root: The path to the test function websock.htm */
+ "document_root",
+ "../../examples/websocket",
+
+ /* port: use http standard to match websocket url in websock.htm:
+ ws://127.0.0.1/MyWebSock */
+ /* if the port is changed here, it needs to be changed in
+ websock.htm as well */
+ "listening_ports",
+ "80",
+
+ NULL};
+
+ callback_funcs.init_context = websock_init_lib;
+ callback_funcs.exit_context = websock_exit_lib;
+
+ ctx = mg_start(&callback_funcs, &ws_ctx, server_options);
+
+ mg_set_websocket_handler(ctx,
+ "/MyWebSock",
+ NULL,
+ websocket_ready_handler,
+ websocket_data_handler,
+ connection_close_handler,
+ NULL);
+
+ printf("Connect to localhost:%s/websock.htm\n",
+ mg_get_option(ctx, "listening_ports"));
+
+ puts("Enter an (ASCII) character or * to exit:");
+ for (;;) {
+ fgets(inbuf, sizeof(inbuf), stdin);
+
+ if (inbuf[0] == '*') {
+ break;
+ }
+ inbuf[0] = toupper(inbuf[0]);
+ websock_send_broadcast(ctx, inbuf, 1);
+ }
+
+ mg_stop(ctx);
+
+ return 0;
+}