websocket chat with floID

This commit is contained in:
sairajzero 2019-04-05 16:47:25 +05:30
parent 52853f64e3
commit e610f984e5
5 changed files with 23433 additions and 0 deletions

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">
publishsubscribe 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>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -0,0 +1,116 @@
/*
* 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 struct mg_serve_http_opts s_http_server_opts;
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));
}
}
void unicast(struct mg_connection *nc, const struct mg_str msg) {
struct mg_connection *c;
char tmp[5000];
char buf[5000];
char addr[32];
char *recipient;
char *actual_msg;
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
//strcpy(tmp,msg.p);
snprintf(tmp, sizeof(tmp), "%.*s",(int)msg.len, msg.p);
printf("%s",tmp);
recipient = strtok(tmp," ");
actual_msg = strtok(NULL,"\0");
printf("%s<%s\n",recipient,actual_msg);
snprintf(buf, sizeof(buf), "%s:%.*s", nc->floID,(int)strlen(actual_msg), actual_msg);
printf("%s<-%s\n",recipient, buf); /* Local echo. */
for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
if (!strcmp(c->floID,recipient)) /* Send to receiver */
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};
if (d.p[0] == '$'){
strcpy(nc->floID,&d.p[1]);
}
else{
unicast(nc, d);
}
break;
}
case MG_EV_HTTP_REQUEST: {
mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts);
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);
s_http_server_opts.document_root = "."; // Serve current directory
s_http_server_opts.enable_directory_listing = "yes";
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;
}