Branch: Tag:

2018-09-28

2018-09-28 13:46:33 by Henrik Grubbström (Grubba) <grubba@grubba.org>

WebSocket: Use WebSocketAPI.

Roxen.upgrade_to_websocket() now takes an api object as a parameter,
this object will then be used for all websocket accesses on the
resulting websocket connection.

30:   //! websocket.   RequestID id;    + //! Object to handle websocket requests. + WebSocketAPI api; +    //! Indicate that we should die.   int is_ended = 0;    - protected void create(RequestID id) { + protected void create(RequestID id, WebSocketAPI api) {    this_program::id = id; -  +  this_program::api = api;    ws_msg_queue = Thread.Queue();    ws_handler_mutex = Thread.Mutex();    onclose = ws_onclose;
75:   protected void ws_handle_queue() {    if (!ws_msg_queue->size()) return;    -  mixed msg = ws_msg_queue->read(); +  string|Protocols.WebSocket.Frame msg = ws_msg_queue->read(); +  if (api) {    if (objectp(msg)) {    // This is a WebSocket frame so let's process it. -  id->conf->websocket_handle(this, msg, "websocket_message"); +  id->json_logger->log(([ +  "event": "WEBSOCKET_MESSAGE_BEGIN", +  "callback": "websocket_message", +  ])); +  api->websocket_message(this, msg); +  id->json_logger->log(([ +  "event": "WEBSOCKET_MESSAGE_END", +  "callback": "websocket_message", +  ]));    } else if (msg == WS_OPEN_MSG) {    // This is the first message so let's call the open callback -  id->conf->websocket_handle(this, msg, "websocket_ready"); +  id->json_logger->log(([ +  "event": "WEBSOCKET_MESSAGE_BEGIN", +  "callback": "websocket_ready", +  ])); +  api->websocket_ready(this); +  id->json_logger->log(([ +  "event": "WEBSOCKET_MESSAGE_END", +  "callback": "websocket_ready", +  ]));    } else if (msg == WS_CLOSE_MSG) { -  id->conf->websocket_handle(this, ws_close_reason, "websocket_close"); +  id->json_logger->log(([ +  "event": "WEBSOCKET_MESSAGE_BEGIN", +  "callback": "websocket_close", +  ])); +  api->websocket_close(this, ws_close_reason); +  id->json_logger->log(([ +  "event": "WEBSOCKET_MESSAGE_END", +  "callback": "websocket_close", +  ]));    id->end();    return;    } -  +  } else if (!is_ended) { +  id->json_logger->log(([ +  "event": "WEBSOCKET_API_HANDLER_GONE", +  ])); +  id->end(); +  is_ended++; +  }       ws_in_handler_queue--;    if (!is_ended && ws_msg_queue->size()) {