简体   繁体   English

如何从客户端只向服务器发送一个“字符”?

[英]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.

相关问题 如何使用Win RPC发送终止的char数组(用于将映像从客户端上传到服务器) - how to use win RPC to send terminated char array(use to upload image from client to server) 当使用 send() 通过 TCP 流将文本文件中的数据从客户端发送到服务器时,如何发送所有数据但一次仅发送 4 个字节? - When using send() to send data from a text file from client to server through TCP stream, how do I send all the data but only 4 bytes at a time? 如何使gRPC服务器仅支持一个客户端连接 - How to enable gRPC server to support just one client connection 我如何只能选择一个对象? - How can i choose just one object? 如何将指向某个程序的指针发送到另一个程序? - How can I send a pointer to something from one program to another? 如何使用 gRPC 在客户端和服务器之间双向发送和接收 stream 元数据? - How can I send and receive stream meta-data bidirectionally between client and server with gRPC? 如何将使用std :: string的xor加密转换为char或char *? - How do I convert xor encryption from using std::string to just char or char *? 我应该使用XML还是Binary将数据从服务器发送到客户端? - Should I use XML or Binary to send data from server to client? 如何从python中获取char * [] - how can I get a char*[] from python C++ 非阻塞发送/接收仅在一侧(客户端或服务器)工作 - C++ non-blocking send/recv work only on one side(client or server)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM