Hello all,
I’ve been trying to set up a WebSocket system using JS. The problem is that the variable becomes undefined after the first frame has passed because it was not defined in that loop. Is there a way to use said variable from the second frame onwards? So I can properly send out my events?
Below is my code relating to the issue.
if (runtimeScene.getTimeManager().isFirstFrame()) {
var socket = new WebSocket(url);
socket.addEventListener("open", (e) => {
response.setString(`Succefully connected.`);
console.log("Success");
socket.addEventListener("message", (e) => {
response.setString(`${e.data}`);
});
});
socket.addEventListener("error", (e) => {
response.setString(`Failed to connect to server.`);
console.log("Failed");
});
}
if (runtimeScene.getGame().getInputManager().isKeyPressed(13)) {
if (textBox.getString() != "") {
sender.setString(``);
socket.send("Test");
// This should send the response back, except socket is undefined.
}
}