简体   繁体   中英

How can I send just only one 'char' from client to server?

I am trying to make an online game. So I need to send the pressed key from client to server. For example, when player presses keys "wwwaasdd" (three times forward, two times left, one times back and two times right). But these keys are not limited. They can be 1000 times. I can send these chars to Server by using write() and Server will take them with readAll(). But it is like "wwwaasddd...". I can store them to QList one by one. But these information will be very much and How can I use them. Could you please tell me correct way? For example in cs go how did programmers do it?

Instead of sending every key input to the server, think about what info you actually need:

"Is the player currently moving or not?"

Your client should handle input and your server should not care or even know about what input is handled and how. In your client when the player presses a key you care about, just change some variable, object state or whatever makes sense for you, eg:

if (buttonPressed == mymoveconfig.forwardButton)
    mymovestate.isMovingForward = true;

Then just send the current state to the server once per frame:

if (mymovestate.hasChangedThisFrame)
    sendToServer(mymovestate);

And on your server receive and handle it:

if (clientHasNewMoveState(i))
    player[i].movestate = receiveMoveStateFromClient(i);

The exact implementation depends on the tools and third party libraries you use and other external factors and would go beyond the scope of this question. Basically: It's up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM