Revert "Stop publish examples to mongoose repo"

This reverts commit 1a17e17c462bdd4e1d26d8742f8b7087273e04c2.

PUBLISHED_FROM=80028de308c9a021955d1425d2bfee8feb85f193
This commit is contained in:
Alexander Alashkin authored and Cesanta Bot committed 2017-02-06 14:08:59 +00:00
1 parent 7cc7df8c0e
commit eaef5bd133
351 files changed
+195424

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
PROG = websocket_chat
MODULE_CFLAGS = -DMG_ENABLE_FILESYSTEM=0
include ../examples.mk
+85
View File
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #789; margin: 0;
padding: 0; font: 14px Helvetica, Arial, sans-serif;
}
div.content {
width: 800px; margin: 2em auto; padding: 20px 50px;
background-color: #fff; border-radius: 1em;
}
#messages {
border: 2px solid #fec; border-radius: 1em;
height: 10em; overflow: scroll; padding: 0.5em 1em;
}
a:link, a:visited { color: #69c; text-decoration: none; }
@media (max-width: 700px) {
body { background-color: #fff; }
div.content {
width: auto; margin: 0 auto; border-radius: 0;
padding: 1em;
}
}
</style>
<script language="javascript" type="text/javascript">
var rooms = [];
var ws = new WebSocket('ws://' + location.host + '/ws');
if (!window.console) { window.console = { log: function() {} } };
ws.onopen = function(ev) { console.log(ev); };
ws.onerror = function(ev) { console.log(ev); };
ws.onclose = function(ev) { console.log(ev); };
ws.onmessage = function(ev) {
console.log(ev);
var div = document.createElement('div');
div.innerHTML = ev.data;
document.getElementById('messages').appendChild(div);
};
window.onload = function() {
document.getElementById('send_button').onclick = function(ev) {
var msg = document.getElementById('send_input').value;
document.getElementById('send_input').value = '';
ws.send(msg);
};
document.getElementById('send_input').onkeypress = function(ev) {
if (ev.keyCode == 13 || ev.which == 13) {
document.getElementById('send_button').click();
}
};
};
</script>
</head>
<body>
<div class="content">
<h1>Websocket PubSub Demonstration</h1>
<p>
This page demonstrates how Mongoose could be used to implement
<a href="http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern">
publish–subscribe pattern</a>. Open this page in several browser
windows. Each window initiates persistent
<a href="http://en.wikipedia.org/wiki/WebSocket">WebSocket</a>
connection with the server, making each browser window a websocket client.
Send messages, and see messages sent by other clients.
</p>
<div id="messages">
</div>
<p>
<input type="text" id="send_input" />
<button id="send_button">Send Message</button>
</p>
</div>
</body>
</html>
+80
View File
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#include "mongoose.h"
static sig_atomic_t s_signal_received = 0;
static const char *s_http_port = "8000";
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); // Reinstantiate signal handler
s_signal_received = sig_num;
}
static int is_websocket(const struct mg_connection *nc) {
return nc->flags & MG_F_IS_WEBSOCKET;
}
static void broadcast(struct mg_connection *nc, const struct mg_str msg) {
struct mg_connection *c;
char buf[500];
char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
snprintf(buf, sizeof(buf), "%s %.*s", addr, (int) msg.len, msg.p);
printf("%s\n", buf); /* Local echo. */
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
if (c == nc) continue; /* Don't send to the sender. */
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, strlen(buf));
}
}
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
switch (ev) {
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
/* New websocket connection. Tell everybody. */
broadcast(nc, mg_mk_str("++ joined"));
break;
}
case MG_EV_WEBSOCKET_FRAME: {
struct websocket_message *wm = (struct websocket_message *) ev_data;
/* New websocket message. Tell everybody. */
struct mg_str d = {(char *) wm->data, wm->size};
broadcast(nc, d);
break;
}
case MG_EV_CLOSE: {
/* Disconnect. Tell everybody. */
if (is_websocket(nc)) {
broadcast(nc, mg_mk_str("-- left"));
}
break;
}
}
}
int main(void) {
struct mg_mgr mgr;
struct mg_connection *nc;
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_set_protocol_http_websocket(nc);
printf("Started on port %s\n", s_http_port);
while (s_signal_received == 0) {
mg_mgr_poll(&mgr, 200);
}
mg_mgr_free(&mgr);
return 0;
}