[英]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).
例如,当玩家按下“wwwaaasdd”键时(向前三次、向左两次、向后一次、向右两次)。 But these keys are not limited.
但这些键不受限制。 They can be 1000 times.
它们可以是 1000 次。 I can send these chars to Server by using write() and Server will take them with readAll().
我可以使用 write() 将这些字符发送到 Server,Server 将使用 readAll() 接收它们。 But it is like "wwwaasddd...".
但这就像“wwwaasddd ...”。 I can store them to QList one by one.
我可以将它们一一存储到 QList 中。 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?
例如在cs go 程序员是如何做到的?
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:
在您的客户端中,当玩家按下您关心的键时,只需更改一些变量 object state 或任何对您有意义的东西,例如:
if (buttonPressed == mymoveconfig.forwardButton)
mymovestate.isMovingForward = true;
Then just send the current state to the server once per frame:然后只需将当前的 state 每帧发送一次到服务器:
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.确切的实现取决于您使用的工具和第三方库以及其他外部因素,并且 go 是否会超出此问题的 scope。 Basically: It's up to you.
基本上:这取决于你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.