summaryrefslogtreecommitdiffhomepage
path: root/src/test/nxt_unit_websocket_chat.c
blob: ec7c2cc330d98bb38279ef4ee4f7a9eaa2c71119 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

/*
 * Copyright (C) NGINX, Inc.
 */

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <sys/mman.h>
#include <sys/stat.h>

#include <nxt_unit.h>
#include <nxt_unit_request.h>
#include <nxt_clang.h>
#include <nxt_websocket.h>
#include <nxt_unit_websocket.h>
#include <nxt_main.h>


#define CONTENT_TYPE    "Content-Type"
#define CONTENT_LENGTH  "Content-Length"
#define TEXT_HTML       "text/html"

typedef struct {
    nxt_queue_link_t  link;
    int               id;
} ws_chat_request_data_t;


static int ws_chat_root(nxt_unit_request_info_t *req);
static void ws_chat_broadcast(const char *buf, size_t size);


static const char     ws_chat_index_html[];
static const int      ws_chat_index_html_size;

static char           ws_chat_index_content_length[34];
static int            ws_chat_index_content_length_size;

static nxt_queue_t    ws_chat_sessions;
static int            ws_chat_next_id = 0;


static void
ws_chat_request_handler(nxt_unit_request_info_t *req)
{
    static char             buf[1024];
    int                     buf_size;
    int                     rc = NXT_UNIT_OK;
    nxt_unit_request_t      *r;
    ws_chat_request_data_t  *data;

    r = req->request;

    const char* target = nxt_unit_sptr_get(&r->target);

    if (strcmp(target, "/") == 0) {
        rc = ws_chat_root(req);
        goto fail;
    }

    if (strcmp(target, "/chat") == 0) {
        if (!nxt_unit_request_is_websocket_handshake(req)) {
            goto notfound;
        }

        rc = nxt_unit_response_init(req, 101, 0, 0);
        if (nxt_slow_path(rc != NXT_UNIT_OK)) {
            goto fail;
        }

        data = req->data;
        nxt_queue_insert_tail(&ws_chat_sessions, &data->link);

        data->id = ws_chat_next_id++;

        nxt_unit_response_upgrade(req);
        nxt_unit_response_send(req);


        buf_size = snprintf(buf, sizeof(buf), "Guest #%d has joined.", data->id);

        ws_chat_broadcast(buf, buf_size);

        return;
    }

notfound:

    rc = nxt_unit_response_init(req, 404, 0, 0);

fail:

    nxt_unit_request_done(req, rc);
}


static int
ws_chat_root(nxt_unit_request_info_t *req)
{
    int rc;

    rc = nxt_unit_response_init(req, 200 /* Status code. */,
                                2 /* Number of response headers. */,
                                nxt_length(CONTENT_TYPE)
                                + nxt_length(TEXT_HTML)
                                + nxt_length(CONTENT_LENGTH)
                                + ws_chat_index_content_length_size
                                + ws_chat_index_html_size);
    if (nxt_slow_path(rc != NXT_UNIT_OK)) {
        return rc;
    }

    rc = nxt_unit_response_add_field(req,
                                     CONTENT_TYPE, nxt_length(CONTENT_TYPE),
                                     TEXT_HTML, nxt_length(TEXT_HTML));
    if (nxt_slow_path(rc != NXT_UNIT_OK)) {
        return rc;
    }

    rc = nxt_unit_response_add_field(req,
                                     CONTENT_LENGTH, nxt_length(CONTENT_LENGTH),
                                     ws_chat_index_content_length,
                                     ws_chat_index_content_length_size);
    if (nxt_slow_path(rc != NXT_UNIT_OK)) {
        return rc;
    }

    rc = nxt_unit_response_add_content(req, ws_chat_index_html,
                                       ws_chat_index_html_size);
    if (nxt_slow_path(rc != NXT_UNIT_OK)) {
        return rc;
    }

    return nxt_unit_response_send(req);
}


static void
ws_chat_broadcast(const char *buf, size_t size)
{
    ws_chat_request_data_t   *data;
    nxt_unit_request_info_t  *req;

    nxt_unit_debug(NULL, "broadcast: %*.s", (int) size, buf);

    nxt_queue_each(data, &ws_chat_sessions, ws_chat_request_data_t, link) {

        req = nxt_unit_get_request_info_from_data(data);

        nxt_unit_req_debug(req, "send: %*.s", (int) size, buf);

        nxt_unit_websocket_send(req, NXT_WEBSOCKET_OP_TEXT, 1, buf, size);
    } nxt_queue_loop;
}


static void
ws_chat_websocket_handler(nxt_unit_websocket_frame_t *ws)
{
    int                     buf_size;
    static char             buf[1024];
    ws_chat_request_data_t  *data;

    if (ws->header->opcode != NXT_WEBSOCKET_OP_TEXT) {
        return;
    }

    data = ws->req->data;

    buf_size = snprintf(buf, sizeof(buf), "Guest #%d: ", data->id);

    buf_size += nxt_unit_websocket_read(ws, buf + buf_size,
                                        nxt_min(sizeof(buf),
                                                ws->content_length));

    ws_chat_broadcast(buf, buf_size);

    nxt_unit_websocket_done(ws);
}


static void
ws_chat_close_handler(nxt_unit_request_info_t *req)
{
    int                     buf_size;
    static char             buf[1024];
    ws_chat_request_data_t  *data;

    data = req->data;
    buf_size = snprintf(buf, sizeof(buf), "Guest #%d has disconnected.",
                        data->id);

    nxt_queue_remove(&data->link);
    nxt_unit_request_done(req, NXT_UNIT_OK);

    ws_chat_broadcast(buf, buf_size);
}


int
main(void)
{
    nxt_unit_ctx_t   *ctx;
    nxt_unit_init_t  init;

    ws_chat_index_content_length_size =
        snprintf(ws_chat_index_content_length,
                 sizeof(ws_chat_index_content_length), "%d",
                 ws_chat_index_html_size);

    nxt_queue_init(&ws_chat_sessions);

    memset(&init, 0, sizeof(nxt_unit_init_t));

    init.callbacks.request_handler = ws_chat_request_handler;
    init.callbacks.websocket_handler = ws_chat_websocket_handler;
    init.callbacks.close_handler = ws_chat_close_handler;

    init.request_data_size = sizeof(ws_chat_request_data_t);

    ctx = nxt_unit_init(&init);
    if (ctx == NULL) {
        return 1;
    }

    nxt_unit_run(ctx);

    nxt_unit_done(ctx);

    return 0;
}


static const char ws_chat_index_html[] =
"<html>\n"
"<head>\n"
"    <title>WebSocket Chat Examples</title>\n"
"    <style type=\"text/css\">\n"
"        input#chat {\n"
"            width: 410px\n"
"        }\n"
"\n"
"        #container {\n"
"            width: 400px;\n"
"        }\n"
"\n"
"        #console {\n"
"            border: 1px solid #CCCCCC;\n"
"            border-right-color: #999999;\n"
"            border-bottom-color: #999999;\n"
"            height: 170px;\n"
"            overflow-y: scroll;\n"
"            padding: 5px;\n"
"            width: 100%;\n"
"        }\n"
"\n"
"        #console p {\n"
"            padding: 0;\n"
"            margin: 0;\n"
"        }\n"
"    </style>\n"
"    <script>\n"
"        \"use strict\";\n"
"\n"
"        var Chat = {};\n"
"\n"
"        Chat.socket = null;\n"
"\n"
"        Chat.connect = (function(host) {\n"
"            if ('WebSocket' in window) {\n"
"                Chat.socket = new WebSocket(host);\n"
"            } else if ('MozWebSocket' in window) {\n"
"                Chat.socket = new MozWebSocket(host);\n"
"            } else {\n"
"                Console.log('Error: WebSocket is not supported by this browser.');\n"
"                return;\n"
"            }\n"
"\n"
"            Chat.socket.onopen = function () {\n"
"                Console.log('Info: WebSocket connection opened.');\n"
"                document.getElementById('chat').onkeydown = function(event) {\n"
"                    if (event.keyCode == 13) {\n"
"                        Chat.sendMessage();\n"
"                    }\n"
"                };\n"
"            };\n"
"\n"
"            Chat.socket.onclose = function () {\n"
"                document.getElementById('chat').onkeydown = null;\n"
"                Console.log('Info: WebSocket closed.');\n"
"            };\n"
"\n"
"            Chat.socket.onmessage = function (message) {\n"
"                Console.log(message.data);\n"
"            };\n"
"        });\n"
"\n"
"        Chat.initialize = function() {\n"
"            var proto = 'ws://';\n"
"            if (window.location.protocol == 'https:') {\n"
"                proto = 'wss://'\n"
"            }\n"
"            Chat.connect(proto + window.location.host + '/chat');\n"
"        };\n"
"\n"
"        Chat.sendMessage = (function() {\n"
"            var message = document.getElementById('chat').value;\n"
"            if (message != '') {\n"
"                Chat.socket.send(message);\n"
"                document.getElementById('chat').value = '';\n"
"            }\n"
"        });\n"
"\n"
"        var Console = {};\n"
"\n"
"        Console.log = (function(message) {\n"
"            var console = document.getElementById('console');\n"
"            var p = document.createElement('p');\n"
"            p.style.wordWrap = 'break-word';\n"
"            p.innerHTML = message;\n"
"            console.appendChild(p);\n"
"            while (console.childNodes.length > 25) {\n"
"                console.removeChild(console.firstChild);\n"
"            }\n"
"            console.scrollTop = console.scrollHeight;\n"
"        });\n"
"\n"
"        Chat.initialize();\n"
"\n"
"      </script>\n"
"</head>\n"
"<body>\n"
"<noscript><h2 style=\"color: #ff0000\">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable\n"
"    Javascript and reload this page!</h2></noscript>\n"
"<div>\n"
"    <p><input type=\"text\" placeholder=\"type and press enter to chat\" id=\"chat\" /></p>\n"
"    <div id=\"container\">\n"
"        <div id=\"console\"/>\n"
"    </div>\n"
"</div>\n"
"</body>\n"
"</html>\n"
;

static const int  ws_chat_index_html_size = nxt_length(ws_chat_index_html);