Skip to content

socket.emit() from the client slows down all threads for 2 milliseconds #376

Open
@Deery50

Description

@Deery50

I'm using this library with LibGDX and I have all of my socket related activity in a different thread from the LibGDX graphics thread. It runs perfectly fine for all received requests but every time I try to emit a new request from the client, every single thread I have (including the graphics thread) freezes for 2 milliseconds (I calculated).
To reiterate, this is when I call "socket.emit(id, data);" from the client. For good measure I've got the code of Client.java below which contains everything I have that deals with this socket library. The issue occurs on line 133 with "socket.emit("move", data);"

EDIT: After Testing around more with this. If the client receives a request very often, the same effect of every thread slowing down occurs.

public class Client implements Runnable {

private GameState gs;
private Socket socket;
private static float UPDATE_TIME = 1/60f;
private static float timer = 0;
private String server = "http://localhost:8080/";

public Client(GameState gs) {
    this.gs = gs;

}

@Override
public void run() {
    try {
        socket = IO.socket(server);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Gdx.app.log("SocketIO", "Connected");
            gs.connectionState = GameState.ConnectionState.DOWNLOADING;
        }
    }).on("init", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            gs.mapDataString = (String) args[0];
            gs.me = gs.newPlayer(true);
            gs.me.x = loadNumberType(args[1]);
            gs.me.y = loadNumberType(args[2]);
            gs.me.id = (String) args[3];

            try {
                JSONArray players = (JSONArray) args[4];
                for(int i = 0; i < players.length(); i++) {
                    JSONObject playerObject = players.getJSONObject(i);
                    String id = playerObject.getString("id");
                    float x = (float) playerObject.getDouble("x");
                    float y = (float) playerObject.getDouble("y");
                    int rotation = playerObject.getInt("rotation");
                    GameState.Player player = gs.newPlayer(false, id, x, y, 0, 0, rotation);
                    gs.getIndexedPlayers().put(id, player);
                    if(!gs.getConnectedPlayerIDS().contains(id))
                        gs.getConnectedPlayerIDS().add(id);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            gs.connectionState = GameState.ConnectionState.READING;
        }
    }).on("newplayer", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            String id = (String) args[2];
            if(!id.equals(gs.me.id)) {
                float x = loadNumberType(args[0]);
                float y = loadNumberType(args[1]);

                GameState.Player newPlayer = gs.newPlayer(false, (String) args[2], x, y, 0, 0, 0);
                gs.getIndexedPlayers().put(id, newPlayer);
                if (!gs.getConnectedPlayerIDS().contains(id))
                    gs.getConnectedPlayerIDS().add(id);
            }
        }
    }).on("delplayer", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            String id = (String) args[0];
            if(gs.getConnectedPlayerIDS().contains(id))
                gs.getConnectedPlayerIDS().remove(id);
            if(gs.getIndexedPlayers().containsKey(id))
                gs.getIndexedPlayers().remove(id);
        }
    }).on("updateplayer", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            try {
                JSONObject updateInfo = (JSONObject) args[0];
                String id = updateInfo.getString("id");
                if(!id.equals(gs.me.id)) {
                    float x = (float) updateInfo.getDouble("x");
                    float y = (float) updateInfo.getDouble("y");
                    int rotation = (int) updateInfo.getInt("rotation");
                    if (gs.getIndexedPlayers().containsKey(id)) {
                        GameState.Player p = gs.getIndexedPlayers().get(id);
                        p.x = x;
                        p.y = y;
                        p.rotation = rotation;
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }).on("getposition", new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            float oldX = loadNumberType(args[0]);
            float oldY = loadNumberType(args[1]);
            int oldRotation = Integer.parseInt(args[2].toString());
            GameState.Player me = gs.me;
            if(me != null && (me.x != oldX || me.y != oldY || me.rotation != oldRotation)) {
                JSONObject data = new JSONObject();
                try {
                    data.put("id", me.id);
                    data.put("x", me.x);
                    data.put("y", me.y);
                    data.put("rotation", me.rotation);
                    data.put("xVector", me.xVector);
                    data.put("yVector", me.yVector);
                    socket.emit("move", data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    socket.connect();
}

private float loadNumberType(Object o) {
    return Float.parseFloat(o.toString());
}

public void dispose() {

}

}
`

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions