You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module. The module is really old, and some design choices had to be re-evaluated. The WebSocketClient and WebSocketServer classes are now gone, and WebSocketPeer can act as either client or server. The WebSocketMultiplayerPeer class is no longer abstract, and implements the Multiplayer API on top of the lower level WebSocketPeer. WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS, so it emits no signal, and just needs polling to update its internal state. To use it as a client, simply call WebSocketPeer.coonect_to_url, then frequently poll the peer until STATE_OPEN is reached and then you can write or read from it, or STATE_CLOSED and then you can check the disconnect code and reason). To implement a server instead, a TCPServer must be created, and the accepted connections needs to be provided to WebSocketPeer.accept_stream (which will perform the HTTP handshake). A full example of a WebSocketServer using TLS will be provided in the demo repository.
This commit is contained in:
@@ -36,27 +36,35 @@ Error RemoteDebuggerPeerWebSocket::connect_to_host(const String &p_uri) {
|
||||
Vector<String> protocols;
|
||||
protocols.push_back("binary"); // Compatibility for emscripten TCP-to-WebSocket.
|
||||
|
||||
ws_client->connect_to_url(p_uri, protocols);
|
||||
ws_client->poll();
|
||||
ws_peer = Ref<WebSocketPeer>(WebSocketPeer::create());
|
||||
ws_peer->set_supported_protocols(protocols);
|
||||
|
||||
if (ws_client->get_connection_status() == WebSocketClient::CONNECTION_DISCONNECTED) {
|
||||
ERR_PRINT("Remote Debugger: Unable to connect. Status: " + String::num(ws_client->get_connection_status()) + ".");
|
||||
ws_peer->set_max_queued_packets(max_queued_messages);
|
||||
ws_peer->set_inbound_buffer_size((1 << 23) - 1);
|
||||
ws_peer->set_outbound_buffer_size((1 << 23) - 1);
|
||||
|
||||
Error err = ws_peer->connect_to_url(p_uri);
|
||||
ERR_FAIL_COND_V(err != OK, err);
|
||||
|
||||
ws_peer->poll();
|
||||
WebSocketPeer::State ready_state = ws_peer->get_ready_state();
|
||||
if (ready_state != WebSocketPeer::STATE_CONNECTING && ready_state != WebSocketPeer::STATE_OPEN) {
|
||||
ERR_PRINT(vformat("Remote Debugger: Unable to connect. State: %s.", ws_peer->get_ready_state()));
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ws_peer = ws_client->get_peer(1);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
bool RemoteDebuggerPeerWebSocket::is_peer_connected() {
|
||||
return ws_peer.is_valid() && ws_peer->is_connected_to_host();
|
||||
return ws_peer.is_valid() && (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN || ws_peer->get_ready_state() == WebSocketPeer::STATE_CONNECTING);
|
||||
}
|
||||
|
||||
void RemoteDebuggerPeerWebSocket::poll() {
|
||||
ws_client->poll();
|
||||
ERR_FAIL_COND(ws_peer.is_null());
|
||||
ws_peer->poll();
|
||||
|
||||
while (ws_peer->is_connected_to_host() && ws_peer->get_available_packet_count() > 0 && in_queue.size() < max_queued_messages) {
|
||||
while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && ws_peer->get_available_packet_count() > 0 && in_queue.size() < max_queued_messages) {
|
||||
Variant var;
|
||||
Error err = ws_peer->get_var(var);
|
||||
ERR_CONTINUE(err != OK);
|
||||
@@ -64,7 +72,7 @@ void RemoteDebuggerPeerWebSocket::poll() {
|
||||
in_queue.push_back(var);
|
||||
}
|
||||
|
||||
while (ws_peer->is_connected_to_host() && out_queue.size() > 0) {
|
||||
while (ws_peer->get_ready_state() == WebSocketPeer::STATE_OPEN && out_queue.size() > 0) {
|
||||
Array var = out_queue[0];
|
||||
Error err = ws_peer->put_var(var);
|
||||
ERR_BREAK(err != OK); // Peer buffer full?
|
||||
@@ -73,7 +81,7 @@ void RemoteDebuggerPeerWebSocket::poll() {
|
||||
}
|
||||
|
||||
int RemoteDebuggerPeerWebSocket::get_max_message_size() const {
|
||||
return 8 << 20; // 8 Mib
|
||||
return ws_peer->get_max_packet_size();
|
||||
}
|
||||
|
||||
bool RemoteDebuggerPeerWebSocket::has_message() {
|
||||
@@ -99,7 +107,6 @@ void RemoteDebuggerPeerWebSocket::close() {
|
||||
if (ws_peer.is_valid()) {
|
||||
ws_peer.unref();
|
||||
}
|
||||
ws_client->disconnect_from_host();
|
||||
}
|
||||
|
||||
bool RemoteDebuggerPeerWebSocket::can_block() const {
|
||||
@@ -111,14 +118,13 @@ bool RemoteDebuggerPeerWebSocket::can_block() const {
|
||||
}
|
||||
|
||||
RemoteDebuggerPeerWebSocket::RemoteDebuggerPeerWebSocket(Ref<WebSocketPeer> p_peer) {
|
||||
#ifdef WEB_ENABLED
|
||||
ws_client = Ref<WebSocketClient>(memnew(EMWSClient));
|
||||
#else
|
||||
ws_client = Ref<WebSocketClient>(memnew(WSLClient));
|
||||
#endif
|
||||
max_queued_messages = (int)GLOBAL_GET("network/limits/debugger/max_queued_messages");
|
||||
ws_client->set_buffers(8192, max_queued_messages, 8192, max_queued_messages);
|
||||
ws_peer = p_peer;
|
||||
if (ws_peer.is_valid()) {
|
||||
ws_peer->set_max_queued_packets(max_queued_messages);
|
||||
ws_peer->set_inbound_buffer_size((1 << 23) - 1);
|
||||
ws_peer->set_outbound_buffer_size((1 << 23) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
RemoteDebuggerPeer *RemoteDebuggerPeerWebSocket::create(const String &p_uri) {
|
||||
|
||||
Reference in New Issue
Block a user