Godot Version
Godot 4.2.1
Question
I’m trying to make a basic connection between Godot and a Node.js backend, but I end up with code -1 errors
Client (on Godot):
extends Node
var socket = WebSocketPeer.new()
const URL = 'wss://locahost:8080/'
func _ready():
socket.connect_to_url(URL)
func _process(delta):
socket.poll()
var state = socket.get_ready_state()
if state == WebSocketPeer.STATE_OPEN:
print('we connected')
elif state == WebSocketPeer.STATE_CLOSING:
pass
elif state == WebSocketPeer.STATE_CLOSED:
var code = socket.get_close_code()
var reason = socket.get_close_reason()
print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1])
this script is attached to a Node
that is the root of a scene, that’s all the setup done
The node.js side if relevant:
const { createServer } = require('http');
const { WebSocketServer } = require('ws');
const server = createServer();
const wss = new WebSocketServer({ server });
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
console.log('someone connected');
});
server.listen(8080, () => {
console.log("Listening on port 8080");
});
I’m using the ws library to create this node.js web socket server, I have tried connecting to this server via browser and via a node.js client using the same library, and they work fine.