1 /** 2 A WebSocket client implementation. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.websocketclient; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.classdb; 24 import godot.websocketmultiplayerpeer; 25 import godot.x509certificate; 26 /** 27 A WebSocket client implementation. 28 29 This class implements a WebSocket client compatible with any RFC 6455-compliant WebSocket server. 30 This client can be optionally used as a network peer for the $(D MultiplayerAPI). 31 After starting the client ($(D connectToUrl)), you will need to $(D NetworkedMultiplayerPeer.poll) it at regular intervals (e.g. inside $(D Node._process)). 32 You will receive appropriate signals when connecting, disconnecting, or when new data is available. 33 */ 34 @GodotBaseClass struct WebSocketClient 35 { 36 package(godot) enum string _GODOT_internal_name = "WebSocketClient"; 37 public: 38 @nogc nothrow: 39 union { /** */ godot_object _godot_object; /** */ WebSocketMultiplayerPeer _GODOT_base; } 40 alias _GODOT_base this; 41 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 42 package(godot) __gshared bool _classBindingInitialized = false; 43 package(godot) static struct GDNativeClassBinding 44 { 45 __gshared: 46 @GodotName("connect_to_url") GodotMethod!(GodotError, String, PoolStringArray, bool, PoolStringArray) connectToUrl; 47 @GodotName("disconnect_from_host") GodotMethod!(void, long, String) disconnectFromHost; 48 @GodotName("get_connected_host") GodotMethod!(String) getConnectedHost; 49 @GodotName("get_connected_port") GodotMethod!(long) getConnectedPort; 50 @GodotName("get_trusted_ssl_certificate") GodotMethod!(X509Certificate) getTrustedSslCertificate; 51 @GodotName("is_verify_ssl_enabled") GodotMethod!(bool) isVerifySslEnabled; 52 @GodotName("set_trusted_ssl_certificate") GodotMethod!(void, X509Certificate) setTrustedSslCertificate; 53 @GodotName("set_verify_ssl_enabled") GodotMethod!(void, bool) setVerifySslEnabled; 54 } 55 /// 56 pragma(inline, true) bool opEquals(in WebSocketClient other) const 57 { return _godot_object.ptr is other._godot_object.ptr; } 58 /// 59 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 60 { _godot_object.ptr = n; return null; } 61 /// 62 pragma(inline, true) bool opEquals(typeof(null) n) const 63 { return _godot_object.ptr is n; } 64 /// 65 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 66 mixin baseCasts; 67 /// Construct a new instance of WebSocketClient. 68 /// Note: use `memnew!WebSocketClient` instead. 69 static WebSocketClient _new() 70 { 71 static godot_class_constructor constructor; 72 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WebSocketClient"); 73 if(constructor is null) return typeof(this).init; 74 return cast(WebSocketClient)(constructor()); 75 } 76 @disable new(size_t s); 77 /** 78 Connects to the given URL requesting one of the given `protocols` as sub-protocol. If the list empty (default), no sub-protocol will be requested. 79 If `true` is passed as `gd_mp_api`, the client will behave like a network peer for the $(D MultiplayerAPI), connections to non-Godot servers will not work, and $(D dataReceived) will not be emitted. 80 If `false` is passed instead (default), you must call $(D PacketPeer) functions (`put_packet`, `get_packet`, etc.) on the $(D WebSocketPeer) returned via `get_peer(1)` and not on this object directly (e.g. `get_peer(1).put_packet(data)`). 81 You can optionally pass a list of `custom_headers` to be added to the handshake HTTP request. 82 $(B Note:) To avoid mixed content warnings or errors in HTML5, you may have to use a `url` that starts with `wss://` (secure) instead of `ws://`. When doing so, make sure to use the fully qualified domain name that matches the one defined in the server's SSL certificate. Do not connect directly via the IP address for `wss://` connections, as it won't match with the SSL certificate. 83 $(B Note:) Specifying `custom_headers` is not supported in HTML5 exports due to browsers restrictions. 84 */ 85 GodotError connectToUrl(in String url, in PoolStringArray protocols = PoolStringArray.init, in bool gd_mp_api = false, in PoolStringArray custom_headers = PoolStringArray.init) 86 { 87 checkClassBinding!(typeof(this))(); 88 return ptrcall!(GodotError)(GDNativeClassBinding.connectToUrl, _godot_object, url, protocols, gd_mp_api, custom_headers); 89 } 90 /** 91 Disconnects this client from the connected host. See $(D WebSocketPeer.close) for more information. 92 */ 93 void disconnectFromHost(in long code = 1000, in String reason = gs!"") 94 { 95 checkClassBinding!(typeof(this))(); 96 ptrcall!(void)(GDNativeClassBinding.disconnectFromHost, _godot_object, code, reason); 97 } 98 /** 99 Return the IP address of the currently connected host. 100 */ 101 String getConnectedHost() const 102 { 103 checkClassBinding!(typeof(this))(); 104 return ptrcall!(String)(GDNativeClassBinding.getConnectedHost, _godot_object); 105 } 106 /** 107 Return the IP port of the currently connected host. 108 */ 109 long getConnectedPort() const 110 { 111 checkClassBinding!(typeof(this))(); 112 return ptrcall!(long)(GDNativeClassBinding.getConnectedPort, _godot_object); 113 } 114 /** 115 116 */ 117 Ref!X509Certificate getTrustedSslCertificate() const 118 { 119 checkClassBinding!(typeof(this))(); 120 return ptrcall!(X509Certificate)(GDNativeClassBinding.getTrustedSslCertificate, _godot_object); 121 } 122 /** 123 124 */ 125 bool isVerifySslEnabled() const 126 { 127 checkClassBinding!(typeof(this))(); 128 return ptrcall!(bool)(GDNativeClassBinding.isVerifySslEnabled, _godot_object); 129 } 130 /** 131 132 */ 133 void setTrustedSslCertificate(X509Certificate arg0) 134 { 135 checkClassBinding!(typeof(this))(); 136 ptrcall!(void)(GDNativeClassBinding.setTrustedSslCertificate, _godot_object, arg0); 137 } 138 /** 139 140 */ 141 void setVerifySslEnabled(in bool enabled) 142 { 143 checkClassBinding!(typeof(this))(); 144 ptrcall!(void)(GDNativeClassBinding.setVerifySslEnabled, _godot_object, enabled); 145 } 146 /** 147 If specified, this $(D X509Certificate) will be the only one accepted when connecting to an SSL host. Any other certificate provided by the server will be regarded as invalid. 148 $(B Note:) Specifying a custom `trusted_ssl_certificate` is not supported in HTML5 exports due to browsers restrictions. 149 */ 150 @property X509Certificate trustedSslCertificate() 151 { 152 return getTrustedSslCertificate(); 153 } 154 /// ditto 155 @property void trustedSslCertificate(X509Certificate v) 156 { 157 setTrustedSslCertificate(v); 158 } 159 /** 160 If `true`, SSL certificate verification is enabled. 161 $(B Note:) You must specify the certificates to be used in the Project Settings for it to work when exported. 162 */ 163 @property bool verifySsl() 164 { 165 return isVerifySslEnabled(); 166 } 167 /// ditto 168 @property void verifySsl(bool v) 169 { 170 setVerifySslEnabled(v); 171 } 172 }