123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include "websocket_client.h"
- GDCINULL(WebSocketClient);
- WebSocketClient::WebSocketClient() {
- verify_ssl = true;
- }
- WebSocketClient::~WebSocketClient() {
- }
- Error WebSocketClient::connect_to_url(String p_url, PoolVector<String> p_protocols, bool gd_mp_api) {
- _is_multiplayer = gd_mp_api;
- String host = p_url;
- String path = "/";
- int p_len = -1;
- int port = 80;
- bool ssl = false;
- if (host.begins_with("wss://")) {
- ssl = true;
- host = host.substr(6, host.length() - 6);
- port = 443;
- } else {
- ssl = false;
- if (host.begins_with("ws://"))
- host = host.substr(5, host.length() - 5);
- }
-
- p_len = host.find("/");
- if (p_len != -1) {
- path = host.substr(p_len, host.length() - p_len);
- host = host.substr(0, p_len);
- }
-
- p_len = host.find_last(":");
- if (p_len != -1 && p_len == host.find(":")) {
- port = host.substr(p_len, host.length() - p_len).to_int();
- host = host.substr(0, p_len);
- }
- return connect_to_host(host, path, port, ssl, p_protocols);
- }
- void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) {
- verify_ssl = p_verify_ssl;
- }
- bool WebSocketClient::is_verify_ssl_enabled() const {
- return verify_ssl;
- }
- bool WebSocketClient::is_server() const {
- return false;
- }
- void WebSocketClient::_on_peer_packet() {
- if (_is_multiplayer) {
- _process_multiplayer(get_peer(1), 1);
- } else {
- emit_signal("data_received");
- }
- }
- void WebSocketClient::_on_connect(String p_protocol) {
- if (_is_multiplayer) {
-
- } else {
- emit_signal("connection_established", p_protocol);
- }
- }
- void WebSocketClient::_on_close_request(int p_code, String p_reason) {
- emit_signal("server_close_request", p_code, p_reason);
- }
- void WebSocketClient::_on_disconnect(bool p_was_clean) {
- if (_is_multiplayer) {
- emit_signal("connection_failed");
- } else {
- emit_signal("connection_closed", p_was_clean);
- }
- }
- void WebSocketClient::_on_error() {
- if (_is_multiplayer) {
- emit_signal("connection_failed");
- } else {
- emit_signal("connection_error");
- }
- }
- void WebSocketClient::_bind_methods() {
- ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api"), &WebSocketClient::connect_to_url, DEFVAL(PoolVector<String>()), DEFVAL(false));
- ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL(""));
- ClassDB::bind_method(D_METHOD("set_verify_ssl_enabled", "enabled"), &WebSocketClient::set_verify_ssl_enabled);
- ClassDB::bind_method(D_METHOD("is_verify_ssl_enabled"), &WebSocketClient::is_verify_ssl_enabled);
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
- ADD_SIGNAL(MethodInfo("data_received"));
- ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
- ADD_SIGNAL(MethodInfo("server_close_request", PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
- ADD_SIGNAL(MethodInfo("connection_closed", PropertyInfo(Variant::BOOL, "was_clean_close")));
- ADD_SIGNAL(MethodInfo("connection_error"));
- }
|