- Edited
Hi, I am having some issues with sending data to a server with websocket.
I am in fact able to connect to the server, and can even receive data:
By using .poll()
inside the _process()
function, and can print the correct data on screen.
The problem lies in using _client.get_peer(1).put_packet()
: nothing happens.
However, if I send the exact same package using javascript, it does give the desired result.
At the bottom is the working javascript code. (Adapted to my needs from a basic example: https://www.websocket.org/echo.html
The most important is: websocket.send("42[\"volume\",30]");
This apparently works.
However, if I use in Godot: _client.get_peer(1).put_packet("42[\"volume\",30]".to_utf8())
Nothing happens.
What makes the difference, how does Godot send the packages? And how can I make it working?
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://192.168.2.16:3000/socket.io/?EIO=4&transport=websocket";
var output;
function init()
{
output = document.getElementById("output");
testWebSocket();
}
function testWebSocket()
{
websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt)
{
writeToScreen("CONNECTED");
doSend("42[\"volume\",30]");
}
function onClose(evt)
{
writeToScreen("DISCONNECTED");
}
function onMessage(evt)
{
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
websocket.close();
}
function onError(evt)
{
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}
function doSend(message)
{
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message)
{
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}
window.addEventListener("load", init, false);
</script>
<div id="output"></div>