Fix EMWSClient::get_connection_status(), try catch

This commit is contained in:
Fabio Alessandrelli 2020-02-15 17:28:52 +01:00
parent 343b29a651
commit ebeeb67224
2 changed files with 24 additions and 13 deletions

View File

@ -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;
};

View File

@ -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 */