diff options
-rw-r--r-- | modules/websocket/emws_client.cpp | 22 | ||||
-rw-r--r-- | modules/websocket/emws_peer.cpp | 15 |
2 files changed, 24 insertions, 13 deletions
diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp index 7e68936fc3..e5680ce2e9 100644 --- a/modules/websocket/emws_client.cpp +++ b/modules/websocket/emws_client.cpp @@ -91,10 +91,14 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, int peer_sock = EM_ASM_INT({ var proto_str = UTF8ToString($2); var socket = null; - if (proto_str) { - socket = new WebSocket(UTF8ToString($1), proto_str.split(",")); - } else { - socket = new WebSocket(UTF8ToString($1)); + try { + if (proto_str) { + socket = new WebSocket(UTF8ToString($1), proto_str.split(",")); + } else { + socket = new WebSocket(UTF8ToString($1)); + } + } catch (e) { + return -1; } var c_ptr = Module.IDHandler.get($0); socket.binaryType = "arraybuffer"; @@ -174,6 +178,8 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, return Module.IDHandler.add(socket); }, _js_id, str.utf8().get_data(), proto_string.utf8().get_data()); /* clang-format on */ + if (peer_sock == -1) + return FAILED; static_cast<Ref<EMWSPeer> >(_peer)->set_sock(peer_sock, _in_buf_size, _in_pkt_size); @@ -190,11 +196,11 @@ Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const { NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const { - if (_peer->is_connected_to_host()) + if (_peer->is_connected_to_host()) { + if (_is_connecting) + return CONNECTION_CONNECTING; return CONNECTION_CONNECTED; - - if (_is_connecting) - return CONNECTION_CONNECTING; + } return CONNECTION_DISCONNECTED; }; diff --git a/modules/websocket/emws_peer.cpp b/modules/websocket/emws_peer.cpp index effed8e4d9..588f38cffd 100644 --- a/modules/websocket/emws_peer.cpp +++ b/modules/websocket/emws_peer.cpp @@ -68,12 +68,17 @@ Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { bytes_array[i] = getValue($1+i, 'i8'); } - if ($3) { - sock.send(bytes_array.buffer); - } else { - var string = new TextDecoder("utf-8").decode(bytes_array); - sock.send(string); + try { + if ($3) { + sock.send(bytes_array.buffer); + } else { + var string = new TextDecoder("utf-8").decode(bytes_array); + sock.send(string); + } + } catch (e) { + return 1; } + return 0; }, peer_sock, p_buffer, p_buffer_size, is_bin); /* clang-format on */ |