socketio / socketio/socket.io

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

Open
#4,328 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs investigation
Dominant language
TypeScript
Stars
63.2k
Forks
10.3k
Avg merge
11d 20h
Merged PRs (30d)
2

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CreateCoreGamePrototype example, especially handleInput(), render(), socket.emit(), and the socket connection setup in createSocket(). Reproduce the CPU increase while sending the UP, DOWN, LEFT, or RIGHT events, then compare CPU and FPS with emitting disabled. Done means identifying whether the issue is in the Java client, the Node.js server, or the event volume and documenting a verified improvement.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, node.js
Domain
game-dev, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.