82061d | 2018-09-28 | Henrik Grubbström (Grubba) | |
#include <module.h>
inherit "module";
constant thread_safe = 1;
constant module_type = MODULE_LOCATION;
constant module_name = "WebSockets: Example module";
constant module_doc =
"This module provides a dummy WebSocket (RFC 6455) service.";
constant module_unique = 0;
|
0ccfb3 | 2018-10-02 | Henrik Grubbström (Grubba) | | #ifdef WEBSOCKET_DEBUG
#define WS_WERR(X...) werror("WebSocket Example: " + X)
#else
#define WS_WERR(X...)
#endif
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | protected void create()
{
defvar("location", "/websocket_example/", "Mount point",
TYPE_LOCATION|VAR_INITIAL|VAR_NO_DEFAULT,
"Where the module will be mounted in the site's virtual "
"file system.");
}
void start(int ignored, Configuration conf)
{
module_dependencies(conf, ({ "websocket" }));
}
string query_name()
{
return sprintf("Example module mounted on %s",
query_location());
}
mapping(string:mixed)|int(0..0) find_file(string path, RequestID id)
{
|
0ccfb3 | 2018-10-02 | Henrik Grubbström (Grubba) | | WS_WERR("find_file(%O, %O) called. Method: %O\n", path, id, id->method);
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | if (id->method != "WebSocketOpen") return 0;
if (path != "") return 0;
return Roxen.upgrade_to_websocket(this, 0);
}
inherit WebSocketAPI;
|
bc7e0b | 2018-10-02 | Henrik Grubbström (Grubba) | | void websocket_ready(WebSocket ws)
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | {
|
0ccfb3 | 2018-10-02 | Henrik Grubbström (Grubba) | | WS_WERR("%O ready. Pending: %d\n", ws->id, ws->id->ws_msg_pending);
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | }
|
bc7e0b | 2018-10-02 | Henrik Grubbström (Grubba) | | void websocket_close(WebSocket ws, Protocols.WebSocket.CLOSE_STATUS reason)
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | {
|
0ccfb3 | 2018-10-02 | Henrik Grubbström (Grubba) | | WS_WERR("%O was closed. Pending: %d\n", ws->id, ws->id->ws_msg_pending);
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | }
|
bc7e0b | 2018-10-02 | Henrik Grubbström (Grubba) | | void websocket_message(WebSocket ws, Protocols.WebSocket.Frame frame)
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | {
|
0ccfb3 | 2018-10-02 | Henrik Grubbström (Grubba) | | WS_WERR("%O(%O, %O): text: %O\n", this_function, ws, frame, frame->text);
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | sscanf(frame->text, "%d %d", int ws_id, int cnt);
if (ws->id->misc->ws_id && ws->id->misc->ws_id != ws_id) {
werror("Wrong WebSocket ID! Expected %d and got %d\n",
ws->id->misc->ws_id, ws_id);
|
bc7e0b | 2018-10-02 | Henrik Grubbström (Grubba) | | return;
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | }
if (ws->id->misc->ws_cnt >= cnt) {
werror("Messages out of order. Last cnt %d, got %d\n",
ws->id->misc->ws_cnt, cnt);
|
bc7e0b | 2018-10-02 | Henrik Grubbström (Grubba) | | return;
|
82061d | 2018-09-28 | Henrik Grubbström (Grubba) | | }
ws->id->misc->ws_id = ws_id;
ws->id->misc->ws_cnt = cnt;
if (!random(10)) {
}
}
|