utils icon indicating copy to clipboard operation
utils copied to clipboard

Error: Decode buffer error: Invalid type 27

Open lazuee opened this issue 2 years ago • 2 comments

image

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);
	});
});

lazuee avatar Oct 06 '22 15:10 lazuee

ooh,. looks like this function, doesn't support godot 4

lazuee avatar Oct 07 '22 05:10 lazuee

hey @lazuee yeah Godot Variant has minimal change on v4 but gd-com is't compatible actually with v4 :( I have to upgrade that, i'm on it :) https://docs.godotengine.org/en/3.5/tutorials/io/binary_serialization_api.html is used there https://docs.godotengine.org/en/latest/tutorials/io/binary_serialization_api.html is for godot 4.0 and they add some type ^^

Tilican avatar Oct 12 '22 14:10 Tilican

@lazuee a version 5.0.0 of gd-com is available :) @gd-com/utils Warning is not compatible with godot 3, for godot 3 use @gd-com/utils 4.1.5

Tilican avatar Jan 04 '23 15:01 Tilican