123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- extends Node
- signal closed
- signal connected
- # The URL we will connect to
- export var websocket_url = "ws://172.31.190.65:8081/ws"
- var receiver = {}
- # Our WebSocketClient instance
- var _client = WebSocketClient.new()
- func send(method = "GET", path="/", body = {}, streamid = ""):
- var msg = {"method": method, "path": path, "body": Marshalls.utf8_to_base64(JSON.print(body)), "streamid": streamid}
- var json_string = JSON.print(msg)
- _client.get_peer(1).put_packet(json_string.to_utf8())
- func reconnect(url = ""):
- if url:
- self.websocket_url = url
- if _client.CONNECTION_CONNECTED:
- return
- var err = _client.connect_to_url(websocket_url)
- if err != OK:
- print("Unable to connect")
- set_process(false)
- else:
- set_process(true)
- func registerReceiver(streamid : String, target):
- receiver[streamid] = target
- func _ready():
- # Connect base signals to get notified of connection open, close, and errors.
- _client.connect("connection_closed", self, "_closed")
- Messaging.registerPublisher("/websocket/connected", self, "connected")
- _client.connect("connection_error", self, "_closed")
- _client.connect("connection_established", self, "_connected")
- Messaging.registerPublisher("/websocket/closed", self, "closed")
- # 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")
- # Initiate connection to the given URL.
- var err = _client.connect_to_url(websocket_url)
- if err != OK:
- print("Unable to connect")
- set_process(false)
- 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("Closed, clean: ", was_clean)
- self.emit_signal("closed")
- set_process(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.
- self.emit_signal("connected")
- _client.get_peer(1).put_packet("Test packet".to_utf8())
- 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 data = _client.get_peer(1).get_packet().get_string_from_utf8()
- print("Got data from server: ", data)
- var result = JSON.parse(data)
- if result.error != OK:
- print("JSON Parse Error: ", result.get_error_string(), " in ", data, " at line ", result.get_error_line())
-
- if not result.result:
- return
- var msg = result.result
- match msg["method"]+":"+msg["path"]:
- "test:/test":
- print(Marshalls.base64_to_utf8(msg["body"]))
- "POST:/v1/point":
- print(Marshalls.base64_to_utf8(msg["body"]))
- "GET:/v1/point/1":
- print(Marshalls.base64_to_utf8(msg["body"]))
- _:
- if receiver.has(msg["streamid"]):
- receiver[msg["streamid"]].receive(msg["streamid"], Marshalls.base64_to_utf8(msg["body"]))
- else:
- print("UNMATCHED HEADER!", msg["method"]+":"+msg["path"])
- print(Marshalls.base64_to_utf8(msg["body"]))
-
- func _process(delta):
- # Call this in _process or _physics_process. Data transfer, and signals
- # emission will only happen when calling this function.
- _client.poll()
|