mirror of
https://github.com/lammertb/libhttp.git
synced 2026-01-03 16:02:30 +03:00
Regorganized directories to make them more intuitive
This commit is contained in:
36
examples/websocket/Makefile
Normal file
36
examples/websocket/Makefile
Normal file
@@ -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 = 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
|
||||
44
examples/websocket/docroot/index.html
Normal file
44
examples/websocket/docroot/index.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8" />
|
||||
<title>WebSocket Test</title>
|
||||
<script language="javascript" type="text/javascript">
|
||||
|
||||
var writeToScreen = function(message) {
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = message;
|
||||
document.getElementById('output').appendChild(div);
|
||||
};
|
||||
window.onload = function() {
|
||||
var url = 'ws://' + window.location.host + '/foo';
|
||||
websocket = new WebSocket(url);
|
||||
websocket.onopen = function(ev) {
|
||||
writeToScreen('CONNECTED');
|
||||
var message = 'Не всё подчиняется разуму. Но всё подчиняется упорству. ';
|
||||
writeToScreen('SENT: ' + message);
|
||||
websocket.send(message);
|
||||
};
|
||||
websocket.onclose = function(ev) {
|
||||
writeToScreen('DISCONNECTED');
|
||||
};
|
||||
websocket.onmessage = function(ev) {
|
||||
writeToScreen('<span style="color: blue;">RESPONSE: ' + ev.data +
|
||||
' </span>');
|
||||
websocket.send('exit');
|
||||
};
|
||||
websocket.onerror = function(ev) {
|
||||
writeToScreen('<span style="color: red; ">ERROR: </span> ' + ev.data);
|
||||
};
|
||||
};
|
||||
</script>
|
||||
<style> div {font: small Verdana; } </style>
|
||||
<h2>Civetweb WebSocket Test</h2>
|
||||
|
||||
<div style="width: 400px; color: #aaa; padding: 1em; ">
|
||||
This page code creates websocket to the URI "/foo",
|
||||
sends a message to it, waits for the reply, then sends an "exit" message.
|
||||
Server must echo all messages back, and terminate the conversation after
|
||||
receiving the "exit" message.
|
||||
</div>
|
||||
|
||||
<div id="output"></div>
|
||||
</html>
|
||||
44
examples/websocket/websocket.c
Normal file
44
examples/websocket/websocket.c
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2004-2012 Sergey Lyubka
|
||||
// This file is a part of civetweb project, http://github.com/sunsetbrew/civetweb
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "civetweb.h"
|
||||
|
||||
static void websocket_ready_handler(struct mg_connection *conn) {
|
||||
static const char *message = "server ready";
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, message, strlen(message));
|
||||
}
|
||||
|
||||
// Arguments:
|
||||
// flags: first byte of websocket frame, see websocket RFC,
|
||||
// http://tools.ietf.org/html/rfc6455, section 5.2
|
||||
// data, data_len: payload data. Mask, if any, is already applied.
|
||||
static int websocket_data_handler(struct mg_connection *conn, int flags,
|
||||
char *data, size_t data_len) {
|
||||
(void) flags; // Unused
|
||||
mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||
|
||||
// Returning zero means stoping websocket conversation.
|
||||
// Close the conversation if client has sent us "exit" string.
|
||||
return memcmp(data, "exit", 4);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_context *ctx;
|
||||
struct mg_callbacks callbacks;
|
||||
const char *options[] = {
|
||||
"listening_ports", "8080",
|
||||
"document_root", "websocket_html_root",
|
||||
NULL
|
||||
};
|
||||
|
||||
memset(&callbacks, 0, sizeof(callbacks));
|
||||
callbacks.websocket_ready = websocket_ready_handler;
|
||||
callbacks.websocket_data = websocket_data_handler;
|
||||
ctx = mg_start(&callbacks, NULL, options);
|
||||
getchar(); // Wait until user hits "enter"
|
||||
mg_stop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user