通过套接字发送坐标,但消息有时会批量在一起,如何防止这种情况?

发布于 2025-01-03 07:16:17 字数 881 浏览 4 评论 0原文

我有一个 AIR 服务器接收消息。我有正在发送消息的 Flash 客户端应用程序。现在我只是对一个客户端进行测试。我担心当我有两个客户端发送坐标时这个问题可能会变得更糟......还没有测试过。

我将客户端的鼠标坐标作为一系列字符串消息(例如“100,200”)发送,然后在服务器端解析它。当用户在客户端上移动鼠标时,我会快速连续地向服务器发送大量此类消息。

大多数时候,当我在服务器上阅读消息时,我会得到一个 x,y...这很棒。有时,当我阅读消息时,坐标会批量处理在一起......

因此,如果我(快速连续)发送

“100,200”

“123,456”

“111,222”

当服务器读取它时,我会看到

“100,200123, 456111,222”!

这是客户端的代码:

socket.writeUTFBytes( coordString ); //an x,y pair
socket.flush(); //make sure it's sent

如何防止这种情况?是因为我连续快速发送消息吗?

注意 通过不同格式发送的建议很好。在这一点上,我只是想保持简单。当用户在客户端上拖动鼠标时,这些消息主要是鼠标坐标。每隔一段时间就会有一条状态消息。所有消息都很短...例如“绿色”、“红色”等。我也可能发送整数,这将减少我发送的字节数。字符串“100”是 3 个字节,但我认为作为一个整数,它可能需要 2 个字节......所以我在那里保存一个字节。此时,当服务器接收鼠标坐标并在服务器屏幕上移动某些内容时,我没有看到服务器上有任何延迟。如果出现问题,我可能会去发送字节。

对于那些发现这个问题并发送更复杂消息的人来说,也许 AMF、JSON、XML 会很好。

I have an AIR server receiving messages. I have Flash client apps that are sending messages. Right now I'm just testing with a single client. I'm afraid that this issue might get worse when I have two clients sending coordinates...haven't tested it out yet.

I'm sending the mouse coordinates of the client as a series of String messages e.g. "100,200" and then parsing it on the server side. When the user is moving the mouse on the client, I'm sending lots of these message in quick succession to the server.

Most of the time on the server when I read the message, I get a single x,y...which is great. Sometimes when I'm reading the message the coordinates are batched together...

So if I'm sending (in quick succession)

"100,200"

"123,456"

"111,222"

When the server reads it I'll see

"100,200123,456111,222"!

Here's the code on the client side:

socket.writeUTFBytes( coordString ); //an x,y pair
socket.flush(); //make sure it's sent

How to I prevent this? Is it because I'm sending messages in quick succession?

NB
The suggestions for potentially sending via a different format are good. At this point, I'm just trying to keep it simple. The messages are mostly mouse coordinates as the user drags their mouse on the client. Every once in awhile there is a status message. All messages are really short...e.g. "GREEN", "RED", etc. I could potentially also send Integers and that will reduce the number of bytes that I'm sending. String "100" is 3 bytes, but I think as an Integer it might take 2 byes...so I'm saving a byte there. At this point, I don't see any lag on the server when it receives the mouse coords and moves something on the server's screen. I might go to sending bytes if it becomes an issue.

For those of you that have found this question and are sending more complex messages, perhaps AMF, JSON, XML would be good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2025-01-10 07:16:17

使用套接字的方式,这就是您想要的效果:链接消息将通信开销降至最低。您必须“打包”每条消息以使其易于区分。例如,如果您有多种消息类型并且需要保持清晰的分隔,则可以将每个单独的消息包装在 XML 节点中,或使用 JSON 表示法。

但是,如果您只想使用坐标,则添加一个简单的分隔符(如 ; 或空格或换行符)就足够了:

100,200;123,456;111,222; 

100,200 123,456 111,222

100,200
123,456
111,222

在服务器端,您必须添加代码来拆分坐标当然,按该字符接收​​字符串并处理每个单独的结果。我可能会选择换行符 - 它们提高了调试的可读性。

The way that sockets are used, this is the effect you desire: Chaining messages keeps the communication overhead to a minimum. You will have to "package" each message to keep them distinguishable. If you have more than one message type and need to keep a clean separation, you could wrap each individual message in an XML node, or use JSON notation, for example.

But if you're only going to use the coordinates, adding a simple separator like ; or a space or line break should be enough:

100,200;123,456;111,222; 

100,200 123,456 111,222

100,200
123,456
111,222

On the server side, you'll have to add code to split the received string by that character and process each individual result, of course. I'd probably go for the line breaks - they improve readability for debugging.

菩提树下叶撕阳。 2025-01-10 07:16:17

这里的问题是您超出了发送范围,而不是等待一个发送完成后再发送下一个。基本上,您是在缓冲区清空之前将其添加到缓冲区中。

因此,要解决此问题,您有 2 个选择。
首先是确保在发送更多数据之前您已经没有发送数据。有点像将它们分批放入数组或其他东西中。
这种方法并不是那么好,因为在服务器端,客户端做什么并不重要。无论客户端发送什么,服务器都应该能够处理它。

现在第二个选项是纠正客户端和服务器。
首先,在客户端上,您需要以空字符结束每次向服务器的发送。这通常是标准,因为它会告诉服务器这是本轮数据的结束。

socket.writeUTFBytes( coordString + String.fromCharCode(0) );

在服务器端,您需要查找该空字符,以便知道何时收到完整的数据包。

The problem here is you are overrunning your sends instead of waiting for one to be complete before send the next. Basically you are adding to the buffer before it is emptied.

So to fix this you have 2 options.
The first would be to make sure you are already not sending data before sending more. Kind of like batching them up in an array or something.
This method would not be all that great because on the server side it shouldn't matter what the client does. No matter what the client send the server should be able to handle it.

Now the second option would be to correct both the client and the server.
First on the client you need to end every send to the server with a null character. This is normally the standard as it would tell the server that this is the end of this round of data.

socket.writeUTFBytes( coordString + String.fromCharCode(0) );

And on the server side you will need to look for that null character so you know when a full data packet was recieved.

梓梦 2025-01-10 07:16:17

不要使用数字的字符串表示形式来发送数字......这是多余的。另外,如果是这种情况,您可能需要阅读 CRC(循环冗余校验)。其他发帖者建议使用现有的编码格式 - 一般来说,这是一个不错的方法,但是,XML、JSON 或 CVS 对于通过网络传输数据来说都是非常糟糕的格式。 CVS 可能最适合您所追求的简单任务,但它仍然是多余的。

如果您可以确定要发送的数字的大小(它们都是固定大小的整数、字节吗?短整型?双精度型?),您可以简单地一个接一个地写入整数,而不必担心丢失或混乱,因为您将始终从套接字读取完全相同长度的字节。还有一些技术可以明确地将可变长度整数写入流,而无需分隔符(Flash 中常用的一种是 UI29)。

但是,如果您计划发送其他类型的数据 - 请查看 AMF 格式。这种格式是专门为支持 ECMAScript 数据结构而设计的,并且与 JSON 不同,它很好地实现了这一目的(JSON 无法表示多对多关系、循环结构,而且它实际上在某种程度上搞乱了字符串格式,最重要的是,太冗长了)。
即使您选择编写服务器的语言没有 AMF 库,编写起来也非常简单,但大多数工业级语言都有 AMF 的一些实现,因此这应该不成问题。

Don't use string presentation of a number to send numbers... that's redundant. Also, you may want to read on CRC (cyclic redundancy check) if that be the case. Other posters suggested using existing encoding formats - this is, in general a good way to go, however, XML, JSON or CVS are all very bad formats for transferring data over the wire. CVS is probably best for the simplistic task you are after, but it's still redundant.

If you can be sure of the size of the numbers you are going to send (are they all integers, of fixed size, bytes? shorts? doubles?) you could simply write one integer after another, never worrying about missing ones or confusion since you will be always reading the exact same length of bytes from the socket. There are also techniques to unambiguously write a variable length integers to a stream w/o the need of separators (one commonly used in Flash is UI29).

However, if you are planning on sending other kinds of data - take a look at AMF format. This format has been specifically designed to support ECMAScript data structures, and, unlike JSON it serves the purpose fairly well (JSON cannot represent many to many relations, cyclical structures, and it had actually messed up the strings format somewhat, on top of that, it is too verbose).
Even if the language you've chosen to write the server doesn't have an AMF library, it is really simple to write, but most industry-level languages have some implementation of AMF, so that should hardly be a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文