socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

socket.emit produces high CPU load [LIBGDX 2d platformer]

Open alexkomazec opened this issue 2 years ago • 1 comments

Describe the bug Hello there, I want to create a multiplayer 2d platformer using socket.io on Client-side (libgdx, Java based). And using node.js on the server side. The problem begins when I try to send some basic emits like socket.emit("UP"); These emits are triggered by clicking some arrows. The code is below:

package com.akomazec.createCoreGamePrototype;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;

/** {@link com.badlogic.gdx.ApplicationListener} implementation shared by all platforms. */
public class CreateCoreGamePrototype extends ApplicationAdapter {
	private SpriteBatch batch;
	private Texture image;
	/* Server stuff */
	private Socket socket;
	private static String url = "http://localhost:8080";

	@Override
	public void create() {
		batch = new SpriteBatch();
		image = new Texture("libgdx.png");
		try {
			createSocket();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		connectSocket();
	}

	public void handleInput() {

		//control our player using immediate impulses
		if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
		{
			socket.emit("UP");
		}

		if (Gdx.input.isKeyJustPressed(Input.Keys.DOWN))
		{
			socket.emit("DOWN");
		}

		if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT))
		{
			socket.emit("LEFT");
		}

		if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT))
		{
			socket.emit("RIGHT");
		}
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(0.15f, 0.15f, 0.2f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
		batch.draw(image, 140, 210);
		batch.end();

		handleInput();
	}

	@Override
	public void dispose() {
		batch.dispose();
		image.dispose();
	}

	private void connectSocket()
	{
		socket.connect();
	}

	private void createSocket() throws URISyntaxException {
		IO.Options options = new IO.Options();
		options.transports = new String[]{"websocket"};
		// Number of failed retries
		options.reconnectionAttempts = 3;
		// Time interval for failed reconnection
		options.reconnectionDelay = 10;
		// Connection timeout (ms)
		options.timeout = 50;
		socket = IO.socket(url, options);

	}
}

From my perspective, I can't provide too much data (By spamming my keyboard buttons) from the client-side, but CPU usage goes up to 30%. Without emitting the data, CPU usage is very low.

NOTE: This is just a sample code. In a real project, idea is just to send important information ( like position vectors). This issue provides FPS drop and lag. Also high CPU usage. Any idea how to overcome that?

alexkomazec avatar Mar 31 '22 22:03 alexkomazec

You're on a wrong project - this is TypeScript and you want Java: https://github.com/socketio/socket.io-client-java

pbadenski avatar Jul 27 '22 12:07 pbadenski