1 /** 2 A class representing a specific WebSocket connection. 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.websocketpeer; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.packetpeer; 24 import godot.reference; 25 /** 26 A class representing a specific WebSocket connection. 27 28 This class represent a specific WebSocket connection, you can do lower level operations with it. 29 You can choose to write to the socket in binary or text mode, and you can recognize the mode used for writing by the other peer. 30 */ 31 @GodotBaseClass struct WebSocketPeer 32 { 33 enum string _GODOT_internal_name = "WebSocketPeer"; 34 public: 35 @nogc nothrow: 36 union { godot_object _godot_object; PacketPeer _GODOT_base; } 37 alias _GODOT_base this; 38 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 39 package(godot) __gshared bool _classBindingInitialized = false; 40 package(godot) static struct _classBinding 41 { 42 __gshared: 43 @GodotName("get_write_mode") GodotMethod!(WebSocketPeer.WriteMode) getWriteMode; 44 @GodotName("set_write_mode") GodotMethod!(void, long) setWriteMode; 45 @GodotName("is_connected_to_host") GodotMethod!(bool) isConnectedToHost; 46 @GodotName("was_string_packet") GodotMethod!(bool) wasStringPacket; 47 @GodotName("close") GodotMethod!(void, long, String) close; 48 @GodotName("get_connected_host") GodotMethod!(String) getConnectedHost; 49 @GodotName("get_connected_port") GodotMethod!(long) getConnectedPort; 50 } 51 bool opEquals(in WebSocketPeer other) const { return _godot_object.ptr is other._godot_object.ptr; } 52 WebSocketPeer opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 53 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 54 mixin baseCasts; 55 static WebSocketPeer _new() 56 { 57 static godot_class_constructor constructor; 58 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WebSocketPeer"); 59 if(constructor is null) return typeof(this).init; 60 return cast(WebSocketPeer)(constructor()); 61 } 62 @disable new(size_t s); 63 /// 64 enum WriteMode : int 65 { 66 /** 67 Specify that WebSockets messages should be transferred as text payload (only valid UTF-8 is allowed). 68 */ 69 writeModeText = 0, 70 /** 71 Specify that WebSockets messages should be transferred as binary payload (any byte combination is allowed). 72 */ 73 writeModeBinary = 1, 74 } 75 /// 76 enum Constants : int 77 { 78 writeModeText = 0, 79 writeModeBinary = 1, 80 } 81 /** 82 Get the current selected write mode. See $(D writemode). 83 */ 84 WebSocketPeer.WriteMode getWriteMode() const 85 { 86 checkClassBinding!(typeof(this))(); 87 return ptrcall!(WebSocketPeer.WriteMode)(_classBinding.getWriteMode, _godot_object); 88 } 89 /** 90 Sets the socket to use the given $(D writemode). 91 */ 92 void setWriteMode(in long mode) 93 { 94 checkClassBinding!(typeof(this))(); 95 ptrcall!(void)(_classBinding.setWriteMode, _godot_object, mode); 96 } 97 /** 98 Returns `true` if this peer is currently connected. 99 */ 100 bool isConnectedToHost() const 101 { 102 checkClassBinding!(typeof(this))(); 103 return ptrcall!(bool)(_classBinding.isConnectedToHost, _godot_object); 104 } 105 /** 106 Returns `true` if the last received packet was sent as a text payload. See $(D writemode) 107 */ 108 bool wasStringPacket() const 109 { 110 checkClassBinding!(typeof(this))(); 111 return ptrcall!(bool)(_classBinding.wasStringPacket, _godot_object); 112 } 113 /** 114 Close this WebSocket connection. `code` is the status code for the closure (see RFC6455 section 7.4 for a list of valid status codes). $(D reason) is the human readable reason for closing the connection (can be any UTF8 string, must be less than 123 bytes). 115 Note: To achieve a clean close, you will need to keep polling until either $(D WebSocketClient.connectionClosed) or $(D WebSocketServer.clientDisconnected) is received. 116 Note: HTML5 export might not support all status codes. Please refer to browsers-specific documentation for more details. 117 */ 118 void close(StringArg1)(in long code = 1000, in StringArg1 reason = "") 119 { 120 checkClassBinding!(typeof(this))(); 121 ptrcall!(void)(_classBinding.close, _godot_object, code, reason); 122 } 123 /** 124 Returns the IP Address of the connected peer. (Not available in HTML5 export) 125 */ 126 String getConnectedHost() const 127 { 128 checkClassBinding!(typeof(this))(); 129 return ptrcall!(String)(_classBinding.getConnectedHost, _godot_object); 130 } 131 /** 132 Returns the remote port of the connected peer. (Not available in HTML5 export) 133 */ 134 long getConnectedPort() const 135 { 136 checkClassBinding!(typeof(this))(); 137 return ptrcall!(long)(_classBinding.getConnectedPort, _godot_object); 138 } 139 }