Hello,
I am trying to set up a wss server using Linux server and HTML5.
First I set up an insecure connection and it worked pretty well, but I can't let it work with wss; I get **ERROR**: Class type: 'X509Certificate' is not instantiable.
Using Godot v3.2.beta.calinou.7f56ef365
Example code:
extends Node
# The URL we will connect to
var websocket_url:String = "wss://82.223.204.60:443"
var connected:bool
# Our WebSocketClient instance
var _client = WebSocketClient.new()
var reconnectTimer:Timer
const reconnectionTimeout:int = 5
func _ready():
# Connect base signals to get notified of connection open, close, and errors.
_client.connect("connection_closed", self, "_closed")
_client.connect("connection_error", self, "_closed")
_client.connect("connection_established", self, "_connected")
# This signal is emitted when not using the Multiplayer API every time
# a full packet is received.
# Alternatively, you could check get_peer(1).get_available_packets() in a loop.
_client.connect("data_received", self, "_on_data")
reconnectTimer = Timer.new()
var certificate:X509Certificate = X509Certificate.new()
print(certificate.load("res://starville.world_ssl_certificate.crt"))
_client.trusted_ssl_certificate = certificate
#_client.verify_ssl = true
# Initiate connection to the given URL.
reconnect()
func reconnect():
print("reconnect, url: " + websocket_url)
var err = _client.connect_to_url(websocket_url)
if err != OK:
print("unable to connect")
set_process(false)
if(!reconnectTimer.get_parent()):
add_child(reconnectTimer)
reconnectTimer.stop()
reconnectTimer.set_wait_time(reconnectionTimeout)
if(!reconnectTimer.is_connected("timeout", self, "reconnect")):
reconnectTimer.connect("timeout", self, "reconnect")
reconnectTimer.start()
else:
print("Connected")
reconnectTimer.stop()
if(reconnectTimer.get_parent()): remove_child(reconnectTimer)
set_process(true)
func _process(delta):
# Call this in _process or _physics_process. Data transfer, and signals
# emission will only happen when calling this function.
if(_client != null): _client.poll()
func _closed(was_clean = false):
# was_clean will tell you if the disconnection was correctly notified
# by the remote peer before closing the socket.
print("Connection closed, clean: ", was_clean)
set_process(false)
if(!reconnectTimer.get_parent()):
add_child(reconnectTimer)
reconnectTimer.stop()
reconnectTimer.set_wait_time(reconnectionTimeout)
if(!reconnectTimer.is_connected("timeout", self, "reconnect")) :
reconnectTimer.connect("timeout", self, "reconnect")
reconnectTimer.start()
connected = false
func _connected(proto = ""):
# This is called on connection, "proto" will be the selected WebSocket
# sub-protocol (which is optional)
print("Connected with protocol: ", proto)
# You MUST always use get_peer(1).put_packet to send data to server,
# and not put_packet directly when not using the MultiplayerAPI.
var varvar = {"hello": "world"}
_client.get_peer(1).put_var(varvar, true)
connected = true
func _on_data():
# Print the received packet, you MUST always use get_peer(1).get_packet
# to receive data from server, and not get_packet directly when not
# using the MultiplayerAPI.
var received = _client.get_peer(1).get_var(true)
print("Got data from server: ", str(received))
func _exit_tree():
_client.disconnect_from_host()