【发布时间】:2012-12-16 04:20:14
【问题描述】:
服务器:有一个服务器保存一组数据,这些数据可以被某个客户端标记(由服务器赋予颜色)。因此,它应该由客户端接收一个事件,处理它,并使用数组的新状态更新所有客户端。该阵列是所有客户端共享的扫雷阶段。
客户端:它有几个按钮,每个按钮都指向数组中的一个位置。每当按下按钮时,它首先与服务器同步(使用Cristian's algorithm),然后向服务器发送一个事件。它还应该随时等待接收更新,当这种情况发生时,按钮也应该更新。
问题:如果我希望客户端永久等待更新并且不与任何其他接收数据混合,我应该怎么做?假设客户端接收到用于同步时钟的Long,那么无论客户端等待接收更新,都不应接收Long。
先谢谢你!
编辑:我使用 TCP 作为我的协议。另外,我添加了一些关于服务器和客户端如何管理事件的伪代码。
我应该用一些伪代码更好地解释这一点:
游戏main()
Create an event handler, and make it run as a separate thread
while amount of players < 2
Receive a connection
Create an instance that represents the player
Send the player some data it will need
Register the player as an observer of the game
end while
for each player
Tell them the game is ready to start
Make the player run in a separate thread
end for each
Mark the game as started
Notify each of the observers about the game
玩家run()
while true
Wait for a time request
Send the player the current time
Wait for a button click
Insert the event into a global list of events
end while
事件处理程序run()
while true
Wait 50 ms
Order the event list by the time marks
Get and remove the first element in the list
Tell the game the button was pressed, also tell it who pressed it
Ask the game to notify the observers
end while
目前,我的客户这样做:
客户main()
Connect to the server
Receive the basic needed data
Wait for a confirmation to start the game
Receive the stage (an array with the positions in the server)
Display one button for each element in the received array
Append an action listener to the button
if the button is pressed
Request the current time to the server
Adjust the time
Send a button click event to the server
Wait to receive the stage again
Update the stage
end if
问题来了。如何将服务器接收时间的时刻和舞台分开,这样两者都不能互相看?我的意思是,服务器可以按任何顺序发送时间或阶段,客户端应该知道它正在接收什么。我迷路了,我不知道该怎么办......
【问题讨论】:
-
呃。介意告诉我为什么我被否决了吗?我在这里迷路了......
-
(我没有投反对票,但是)很难用这个高级/抽象的问题有意义地回答。您没有显示任何代码或伪代码,也没有提及您正在使用的任何套接字协议(比 TCP/UDP 更高级别的任何协议?)。您提到了诸如“颜色”和“按钮”(完全抽象)之类的东西,还谈到了解决更具体的时间同步问题。
-
哦!好吧,好点,我忘了展示一些代码(尽管我需要的是一般的想法,而不是确切的代码)。但我可以指定一些事情。谢谢,我马上解决。
-
最重要的是,我很难理解你在最后一句话中的意思。您是说您希望服务器和每个客户端之间有两个独立的通信渠道,因此“一般”数据不会与“时间同步”数据混合吗?如果是这样,那么你需要设计这个,或者使用一个协议来为你做那个。
-
我也很难用语言表达。但我会用一些伪代码更好地解释这一点。
标签: java sockets asynchronous synchronization