websocket.c (1959B)
1 /*
2 * Copyright (c) 2014 Joris Vink <joris@coders.se>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <kore/kore.h>
18 #include <kore/http.h>
19
20 #include "assets.h"
21
22 int page(struct http_request *);
23 int page_ws_connect(struct http_request *);
24
25 void websocket_connect(struct connection *);
26 void websocket_disconnect(struct connection *);
27 void websocket_message(struct connection *,
28 u_int8_t, void *, size_t);
29
30 /* Called whenever we get a new websocket connection. */
31 void
32 websocket_connect(struct connection *c)
33 {
34 kore_log(LOG_NOTICE, "%p: connected", c);
35 }
36
37 void
38 websocket_message(struct connection *c, u_int8_t op, void *data, size_t len)
39 {
40 kore_websocket_broadcast(c, op, data, len, WEBSOCKET_BROADCAST_GLOBAL);
41 }
42
43 void
44 websocket_disconnect(struct connection *c)
45 {
46 kore_log(LOG_NOTICE, "%p: disconnecting", c);
47 }
48
49 int
50 page(struct http_request *req)
51 {
52 http_response_header(req, "content-type", "text/html");
53 http_response(req, 200, asset_frontend_html, asset_len_frontend_html);
54
55 return (KORE_RESULT_OK);
56 }
57
58 int
59 page_ws_connect(struct http_request *req)
60 {
61 /* Perform the websocket handshake, passing our callbacks. */
62 kore_websocket_handshake(req, "websocket_connect",
63 "websocket_message", "websocket_disconnect");
64
65 return (KORE_RESULT_OK);
66 }