-4

服务器:有一个服务器保存一组数据,这些数据可以被某个客户端标记(服务器给它一个颜色)。因此,它应该由客户端接收一个事件,处理它,并使用数组的新状态更新所有客户端。该阵列是所有客户共享的扫雷阶段。

客户端:它有几个按钮,每个按钮都指向数组中的一个位置。每当按下按钮时,它首先与服务器同步(使用Cristian 算法),然后向服务器发送一个事件。它还应该随时等待接收更新,当这种情况发生时,按钮也应该更新。

问题:如果我希望客户端永久等待更新并且不与任何其他接收数据混合,我该怎么办?假设客户端收到一个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   

问题来了。如何将服务器接收时间的时刻和舞台分开,这样两者都不能互相看?我的意思是,服务器可以按任何顺序发送时间或阶段,客户端应该知道它正在接收什么。我迷路了,我不知道该怎么办......

4

1 回答 1

1

您是否查看或考虑过 JMS?您可以参考JAVA EE 6 DOC开始使用 JMS。

于 2012-12-16T05:13:23.017 回答