static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; struct websocket_message *wm = (struct websocket_message *) ev_data; switch (ev) { case NS_HTTP_REQUEST: /* Usual HTTP request - serve static files */ ns_serve_http(nc, hm, s_http_server_opts); nc->flags |= NSF_SEND_AND_CLOSE; break; case NS_WEBSOCKET_HANDSHAKE_DONE: /* New websocket connection. Tell everybody. */ broadcast(nc, "joined", 6); break; case NS_WEBSOCKET_FRAME: /* New websocket message. Tell everybody. */ broadcast(nc, (char *) wm->data, wm->size); break; case NS_CLOSE: /* Disconnect. Tell everybody. */ if (is_websocket(nc)) { broadcast(nc, "left", 4); } break; default: break; } }
static void cb1(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; if (ev == NS_HTTP_REQUEST) { if (ns_vcmp(&hm->uri, "/foo") == 0) { ns_printf(nc, "HTTP/1.0 200 OK\n\n[%.*s %d]", (int) hm->uri.len, hm->uri.p, (int) hm->body.len); nc->flags |= NSF_FINISHED_SENDING_DATA; } else { static struct ns_serve_http_opts opts; opts.document_root = "."; ns_serve_http(nc, hm, opts); } } }
static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case NS_HTTP_REQUEST: if (ns_vcmp(&hm->uri, "/api/v1/sum") == 0) { handle_sum_call(nc, hm); /* Handle RESTful call */ } else { ns_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } nc->flags |= NSF_FINISHED_SENDING_DATA; break; default: break; } }
static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case NS_HTTP_REQUEST: if (ns_vcmp(&hm->uri, "/api/v1/sum") == 0) { handle_sum_call(nc, hm); /* Handle RESTful call */ } else if (ns_vcmp(&hm->uri, "/printcontent") == 0) { char buf[100] = {0}; memcpy(buf, hm->body.p, sizeof(buf) - 1 < hm->body.len? sizeof(buf) - 1 : hm->body.len); printf("%s\n", buf); } else { ns_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } break; default: break; } }