gd-com / utils Goto Github PK
View Code? Open in Web Editor NEWBinary serialization helper godot and nodejs !
License: MIT License
Binary serialization helper godot and nodejs !
License: MIT License
Hi, I am having troubles with UDP packets being received on my server. I send packets at a constant rate on the client but on the NodeJS server they were being received in bursts. The on Message event freezes up regularly and then keep going.
Server code: https://pastebin.com/uZCmhdTC
Client code: https://pastebin.com/HeuuLuKH
OS: PopOS 20.04 LTS
Node version: 10.19.0
NPM version: 6.14.4
Thanks for any help, this has been bugging me a lot. And I feel like I must be making some stupid mistake.
Hi!
I've been working with gd-com this past month and I recently found myself stumped. I'm trying to send an array of JSON over WS but the get_var() function from Godot 3.4.2 seems to be parsing it as the wrong type.
The type it's being recognized at seems to change according to the contents of the array itself.
Here's a (shortened) version of the code I'm running:
Node:
const action = putU8(7)
const list = putVar([{'a':1},{'b':2},{'c':3}], TYPE.ARRAY)
const data = Buffer.concat([action, list])
ws.send(answer.data)
Godot:
var packet := _peer.get_packet()
var buffer := StreamPeerBuffer.new()
buffer.set_data_array(packet)
var action := buffer.get_u8()
match action:
7:
var list = buffer.get_var()
# at this point, 'list' is parsed as null, a float, or an int, depending on the contents of the array
Considering that last detail, my guess would be that I'm either doing something wrong, or there's some mistake in the way the Array type is being formatted in @gd-com, leading to the type being incorrectly parsed.
package.json
{
"name": "server",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"start": "ts-node ./src/server.ts",
"start:dev": "nodemon src/server.ts",
"start:debug": "nodemon --inspect src/server.ts",
"start:prod": "yarn build build && node ./dist/src/server.js",
"build": "tsc",
"clean": "rimraf build"
},
"devDependencies": {
"@types/express": "^4.17.14",
"@types/node": "^18.8.2",
"@types/ws": "^8.5.3",
"nodemon": "^2.0.20",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"dependencies": {
"@gd-com/utils": "^4.1.4",
"@types/uuid": "^8.3.4",
"dotenv": "^16.0.3",
"express": "^4.18.1",
"rimraf": "^3.0.2",
"tslib": "^2.4.0",
"uuid": "^9.0.0",
"ws": "^8.9.0"
}
}
main.gd
func send(cmd: String, data: Dictionary) -> void:
_client.get_peer(1).put_var({ "cmd": cmd, "data": data })
code:
import "dotenv/config"
import WebSocket from "ws";
import Express from "express";
import { getVar, putVar } from "@gd-com/utils";
export const app = Express();
app.set("port", process.env.PORT || 3000)
app.set("query parser", "extended")
app.set("trust proxy", 1)
app.set("json spaces", 2)
app.disable("x-powered-by")
app.use(express.urlencoded({ extended: false, limit: "2mb" }))
app.use(express.json({ limit: "2mb" }))
const server = app.listen(app.get("port"), async () => {
console.log("Server listening on port: " + process.env.PORT);
});
const wss = new WebSocket.Server({ server });
const print = (data: any) => console.log(data)
wss.on("connection", (socket) => {
// Send data to Client
const send = (data = { cmd: "null", content: {} }) => {
socket.send(putVar(data));
};
// Broadcast data to all Client
const broadcast = (data = { cmd: "null", content: {} }) => {
wss.clients.forEach((client) => {
if (client !== socket && client.readyState === WebSocket.OPEN) {
client.send(putVar(data));
}
});
};
socket.on("message", (message) => {
/**
* Error on 'getVar'
**/
const variant = getVar(Buffer.from(message as any));
switch (variant.value.cmd) {
case "login": print(variant)
break;
default:
break;
}
});
// On client error
socket.on("error", (err) => {
console.error(err);
});
// When client disconnects
socket.on("close", (code, reason) => {
console.log("Client disconnected - ", code, reason);
});
});
It's time to make it secured !
TLS is available on Godot 3.0.X +
DTLS going to be available on Godot 3.2
Websocket can be secured with apache or nginx certificat
Tutorial is still there, just changed address.
https://www.npmjs.com/package/gd-com
It says this package is deprecated.
Really? Why? Is there an alternative? Why does it say so only on npmjs and not on github?
Godot 3.1, not sure if it's a bug or what, the following works
func _process(delta):
if ws.get_connection_status() == ws.CONNECTION_CONNECTING || ws.get_connection_status() == ws.CONNECTION_CONNECTED:
ws.poll()
ws.get_peer(1).put_var('hi')
but if you remove it from delta and put it in ready function, it won't work, no messages will be sent to the server.
And if you do this, it won't work also
var joined_game = false
func _process(delta):
if ws.get_connection_status() == ws.CONNECTION_CONNECTING || ws.get_connection_status() == ws.CONNECTION_CONNECTED:
ws.poll()
if joined_game == false:
ws.get_peer(1).put_var('hi')
joined_game = true
Here only one message should be sent, therefore it won't work, not sure why. This is unrelated to the library, but you already know about the issue, because in your examples you always send the message, but I think in your advanced example, you didn't face the issue, did you?
Is it a bug in my code or a bug in Godot, that's all I want to know, thank you and sorry for bothering you. You know a lot more about Godot websockets than many people including me, that's why I'm asking.
Awesome library! I just found it when looking into Godot's websocket stuff. Is there a reason all of the methods are wrapped as async
? It looks like everything being done is synchronous.
Keep up the great work!
When testet put/get only on nodejs, it's fork fine, but when test Your advanced example websocket, and change line 37 buffer.put_string('an extra parameter')
on buffer.put_var('an extra parameter') it return wrong data "♦ ↕ an extra parameter "
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.