Godot Version
4.2.1
Question
I am trying to connect my game to a local Node.js websocket server I am running on the same machine. I have verified that my nodejs ws server is working correctly, as I am able to connect to it with an angular web server. My end goal is to create a game similar to the Jackbox games in terms of architecture.
However, when the sever is running and I try to connect my godot game client to the server, it hangs forever on “connecting”. meaning the GetReadyState() function of the WebSocketPeer object is hanging on the connection state. I get no connection logs from my nodejs server that it connected either.
Here is my nodejs server script:
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const app = express();
const PORT = process.env.PORT || 3000;
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST", "WS"]
}
})
io.on('connection', (socket) => {
console.log("CLIENT CONNECTED");
socket.on('message', (message) => {
console.log(`Received message from client ${socket}: ${message}`);
io.emit('message', message);
})
socket.on('disconnect', () => {
console.log('CLIENT DISCONNECTED');
})
})
server.listen(PORT, '192.168.0.28', () => {
console.log("SERVER IS RUNNING ON http://192.168.0.28:3000");
})
And here is my c# script attached to a node object in an empty scene in godot:
using Godot;
using System;
public partial class WebsocketTest : Node3D
{
const string url = "ws://192.168.0.28:3000/";
WebSocketPeer webSocketPeer = new WebSocketPeer();
public override void _Ready()
{
Error error = webSocketPeer.ConnectToUrl(url);
if (error != Error.Ok)
{
GD.Print("Unable to connect to websocket server at " + url);
SetProcess(false);
}
else
{
GD.Print("Connected to server at " + url);
}
}
public override void _Process(double delta)
{
webSocketPeer.Poll();
GD.Print(webSocketPeer.GetReadyState());
}
}
The ready state of the websocket connection in Godot is always connecting: