mirror of
https://github.com/cesanta/mongoose.git
synced 2024-11-25 03:29:00 +08:00
Moved websocket echo server example
This commit is contained in:
parent
b5f6254c1e
commit
cfd334ac6c
12
examples/websocket_echo_server/Makefile
Normal file
12
examples/websocket_echo_server/Makefile
Normal file
@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2014 Cesanta Software
|
||||
# All rights reserved
|
||||
|
||||
PROG = websocket_echo_server
|
||||
CFLAGS = -W -Wall -I../.. -g -O0 $(CFLAGS_EXTRA)
|
||||
SOURCES = $(PROG).c ../../mongoose.c
|
||||
|
||||
$(PROG): $(SOURCES)
|
||||
$(CC) -o $(PROG) $(SOURCES) $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -rf $(PROG) *.exe *.dSYM *.obj *.exp .*o *.lib
|
@ -1,27 +1,24 @@
|
||||
// Copyright (c) 2013-2014 Cesanta Software Limited
|
||||
// $Date: 2014-09-09 08:27:35 UTC $
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "mongoose.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define snprintf _snprintf
|
||||
#endif
|
||||
static void push_message(struct mg_server *server, time_t current_time) {
|
||||
struct mg_connection *c;
|
||||
char buf[20];
|
||||
int len = sprintf(buf, "%lu", (unsigned long) current_time);
|
||||
|
||||
extern const char *find_embedded_file(const char *, size_t *);
|
||||
|
||||
static int iterate_callback(struct mg_connection *c, enum mg_event ev) {
|
||||
if (ev == MG_POLL && c->is_websocket) {
|
||||
char buf[20];
|
||||
int len = snprintf(buf, sizeof(buf), "%lu",
|
||||
(unsigned long) * (time_t *) c->callback_param);
|
||||
mg_websocket_write(c, 1, buf, len);
|
||||
// Iterate over all connections, and push current time message to websocket ones.
|
||||
for (c = mg_next(server, NULL); c != NULL; c = mg_next(server, c)) {
|
||||
if (c->is_websocket) {
|
||||
mg_websocket_write(c, 1, buf, len);
|
||||
}
|
||||
}
|
||||
return MG_TRUE;
|
||||
}
|
||||
|
||||
static int send_reply(struct mg_connection *conn) {
|
||||
size_t index_size;
|
||||
const char *index_html = find_embedded_file("websocket.html", &index_size);
|
||||
|
||||
if (conn->is_websocket) {
|
||||
// This handler is called for each incoming websocket frame, one or more
|
||||
// times for connection lifetime.
|
||||
@ -30,19 +27,16 @@ static int send_reply(struct mg_connection *conn) {
|
||||
return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ?
|
||||
MG_FALSE : MG_TRUE;
|
||||
} else {
|
||||
mg_send_header(conn, "Content-Type", "text/html");
|
||||
mg_send_data(conn, index_html, index_size);
|
||||
return MG_TRUE;
|
||||
mg_send_file(conn, "index.html");
|
||||
return MG_MORE;
|
||||
}
|
||||
}
|
||||
|
||||
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
||||
if (ev == MG_REQUEST) {
|
||||
return send_reply(conn);
|
||||
} else if (ev == MG_AUTH) {
|
||||
return MG_TRUE;
|
||||
} else {
|
||||
return MG_FALSE;
|
||||
switch (ev) {
|
||||
case MG_AUTH: return MG_TRUE;
|
||||
case MG_REQUEST: return send_reply(conn);
|
||||
default: return MG_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,7 +52,7 @@ int main(void) {
|
||||
current_timer = time(NULL);
|
||||
if (current_timer - last_timer > 0) {
|
||||
last_timer = current_timer;
|
||||
mg_iterate_over_connections(server, iterate_callback, ¤t_timer);
|
||||
push_message(server, current_timer);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user