Making any real-time collaborative tool/game boils down to efficiently synchronizing changes on a minimal shared data structure between clients. Network bandwidth is the bottleneck. Send only information absolutely needed to synchronize the shared data. You are on the right track by storing shapes instead of individual pixels. However, the shapes should not handle mouse events. As you noted, broadcasting mouse events will quickly saturate the network bandwidth! Instead, pass deltas of how the shapes are altered by the mouse events. For example, instead of sending mouse_motion() send the final position [x,y] after a shape has been moved.
I suggest splitting your drawing program into a server part and client part. The server keeps the authoritative version of the shared data. A client never manipulates the shared data structure directly; it only sends network messages to the server. This may seem silly when both the client and server are in the same process/PC, but there are some good reasons:
Shared code path for both single-user and multi-user
The network overhead between a client and server in the same process is next to zero when using local sockets
In addition, editing does not have to be limited to the owner of that shape. Since the server is the final authority, it resolves any conflicts when two people grab the same shape simultaneously and send the results back to the clients. (Undo gets a little tricky, though.)
Although a centralized server is best for network discovery, clients can use other methods to find a server:
Connect directly via an IP address. (The server IP address will have to be communicated via other means: chat, cell phone, shouting across the room, carrier pigeon, ...)
Finally, look at how other multi-user applications are designed. Here are some examples:
Zoidcom Multi-player game programming libary (C++). Much of this answer is based on information from Zoidcom documentation. There are even sample programs that demonstrate server discovery via network broadcast.
发布评论
评论(1)
制作任何实时协作工具/游戏都可以归结为在客户端之间有效同步最小共享数据结构上的更改。网络带宽是瓶颈。仅发送同步共享数据绝对需要的信息。通过存储形状而不是单个像素,您走在正确的轨道上。但是,形状不应处理鼠标事件。正如您所指出的,广播鼠标事件将很快使网络带宽饱和!相反,传递鼠标事件如何改变形状的增量。例如,在形状移动后发送最终位置 [x,y],而不是发送 mouse_motion()。
我建议将您的绘图程序分成服务器部分和客户端部分。服务器保留共享数据的权威版本。客户端从不直接操作共享数据结构;它只向服务器发送网络消息。当客户端和服务器位于同一进程/PC 中时,这可能看起来很愚蠢,但有一些充分的理由:
此外,编辑不必仅限于该形状的所有者。由于服务器是最终权威,因此当两个人同时抓取相同的形状并将结果发送回客户端时,它会解决任何冲突。 (不过,撤消操作有点棘手。)
虽然集中式服务器最适合网络发现,但客户端可以使用其他方法来查找服务器:
最后,看看其他多用户应用程序是如何设计的。以下是一些示例:
Making any real-time collaborative tool/game boils down to efficiently synchronizing changes on a minimal shared data structure between clients. Network bandwidth is the bottleneck. Send only information absolutely needed to synchronize the shared data. You are on the right track by storing shapes instead of individual pixels. However, the shapes should not handle mouse events. As you noted, broadcasting mouse events will quickly saturate the network bandwidth! Instead, pass deltas of how the shapes are altered by the mouse events. For example, instead of sending mouse_motion() send the final position [x,y] after a shape has been moved.
I suggest splitting your drawing program into a server part and client part. The server keeps the authoritative version of the shared data. A client never manipulates the shared data structure directly; it only sends network messages to the server. This may seem silly when both the client and server are in the same process/PC, but there are some good reasons:
In addition, editing does not have to be limited to the owner of that shape. Since the server is the final authority, it resolves any conflicts when two people grab the same shape simultaneously and send the results back to the clients. (Undo gets a little tricky, though.)
Although a centralized server is best for network discovery, clients can use other methods to find a server:
Finally, look at how other multi-user applications are designed. Here are some examples: