summaryrefslogtreecommitdiff
path: root/modules/websocket/emws_client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/websocket/emws_client.cpp')
-rw-r--r--modules/websocket/emws_client.cpp41
1 files changed, 24 insertions, 17 deletions
diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp
index 00c36ebb47..82a577790e 100644
--- a/modules/websocket/emws_client.cpp
+++ b/modules/websocket/emws_client.cpp
@@ -31,6 +31,7 @@
#include "emws_client.h"
#include "core/io/ip.h"
+#include "core/project_settings.h"
#include "emscripten.h"
extern "C" {
@@ -43,8 +44,9 @@ EMSCRIPTEN_KEEPALIVE void _esws_on_connect(void *obj, char *proto) {
EMSCRIPTEN_KEEPALIVE void _esws_on_message(void *obj, uint8_t *p_data, int p_data_size, int p_is_string) {
EMWSClient *client = static_cast<EMWSClient *>(obj);
- static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1);
- client->_on_peer_packet();
+ Error err = static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1);
+ if (err == OK)
+ client->_on_peer_packet();
}
EMSCRIPTEN_KEEPALIVE void _esws_on_error(void *obj) {
@@ -55,32 +57,31 @@ EMSCRIPTEN_KEEPALIVE void _esws_on_error(void *obj) {
EMSCRIPTEN_KEEPALIVE void _esws_on_close(void *obj, int code, char *reason, int was_clean) {
EMWSClient *client = static_cast<EMWSClient *>(obj);
+ client->_on_close_request(code, String(reason));
client->_is_connecting = false;
- client->_on_disconnect();
+ client->_on_disconnect(was_clean != 0);
}
}
Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) {
+ String proto_string = p_protocols.join(",");
String str = "ws://";
- String proto_string = "";
if (p_ssl)
str = "wss://";
str += p_host + ":" + itos(p_port) + p_path;
- for (int i = 0; i < p_protocols.size(); i++) {
- proto_string += p_protocols[i];
- proto_string += ",";
- }
- if (proto_string == "")
- proto_string = "binary,";
-
- proto_string = proto_string.substr(0, proto_string.length() - 1);
_is_connecting = true;
/* clang-format off */
int peer_sock = EM_ASM_INT({
- var socket = new WebSocket(UTF8ToString($1), UTF8ToString($2).split(","));
+ 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));
+ }
var c_ptr = Module.IDHandler.get($0);
socket.binaryType = "arraybuffer";
@@ -147,7 +148,7 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
if (!Module.IDHandler.has($0))
return; // Godot Object is gone!
var was_clean = 0;
- if (event.was_clean)
+ if (event.wasClean)
was_clean = 1;
ccall("_esws_on_close",
"void",
@@ -160,7 +161,7 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
}, _js_id, str.utf8().get_data(), proto_string.utf8().get_data());
/* clang-format on */
- static_cast<Ref<EMWSPeer> >(_peer)->set_sock(peer_sock);
+ static_cast<Ref<EMWSPeer> >(_peer)->set_sock(peer_sock, _in_buf_size, _in_pkt_size);
return OK;
};
@@ -184,9 +185,9 @@ NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() c
return CONNECTION_DISCONNECTED;
};
-void EMWSClient::disconnect_from_host() {
+void EMWSClient::disconnect_from_host(int p_code, String p_reason) {
- _peer->close();
+ _peer->close(p_code, p_reason);
};
IP_Address EMWSClient::get_connected_host() const {
@@ -199,7 +200,13 @@ uint16_t EMWSClient::get_connected_port() const {
return 1025;
};
+int EMWSClient::get_max_packet_size() const {
+ return (1 << _in_buf_size) - PROTO_SIZE;
+}
+
EMWSClient::EMWSClient() {
+ _in_buf_size = GLOBAL_GET(WSC_IN_BUF);
+ _in_pkt_size = GLOBAL_GET(WSC_IN_PKT);
_is_connecting = false;
_peer = Ref<EMWSPeer>(memnew(EMWSPeer));
/* clang-format off */