54
Using Media Classes
Assign an owner
A more complex strategy is to define a single client as the owner of a
property in a shared object for a limited period of time. You might write server code to create
a “lock” object, where a client can request ownership of a slot. If the server reports that the
request was successful, the client knows that it will be the only client changing the data in the
shared object.
The example that follows is server-side ActionScript that locks and unlocks a shared object to
make sure the true highest score is returned to the game. Suppose the most recent high score
was 95, and player 1’s score increased to 105, while player 2’s score increased to 110. If no
locking occurred, both players’ scores could be compared to the most recent high score, 95, or
a collision could take place if both clients call
updateHighScore
simultaneously. You’d want
to make sure that each player was compared to the highest score, regardless of whether it was
the most recent high score or any score currently coming in from all other clients. If you lock
and unlock the shared object used to store the highest score, you can compare each score
sequentially, and thus ensure that no comparison is lost.
application.onAppStart = function()
{
application.scoreSO = SharedObject.get("high_score_so", true);
application.scoreSO.onSync = function(listVal)
{
trace("got an onSync on scoreSO");
}
}
application.onConnect = function(newClient,name,passwd)
{
newClient.updateHighScore = function(final_score)
{
application.scoreSO.lock();
if (application.scoreSO.getProperty("high_score_so") < final_score)
{
application.scoreSO.setProperty("high_score_so", final_score);
}
application.scoreSO.unlock();
}
}
Notify the client
When the server rejects a client-requested change to a property of the
shared object, the
SharedObject.onSync
event handler notifies the client that the change
was rejected. Thus, an application can provide a user interface to let a user resolve the conflict.
This technique works best if data is changed infrequently, as in a shared address book. If a
synchronization conflict occurs, the user can decide whether to accept or reject the change.
Содержание FLASH MEDIA SERVER 2-DEVELOPING MEDIA
Страница 1: ...Developing Media Applications ...
Страница 6: ...6 ...
Страница 10: ...10 About This Manual ...
Страница 36: ...36 Flash Media Server Architecture ...
Страница 80: ...80 Debugging and Monitoring Applications ...
Страница 106: ...106 Application Development Tips and Tricks ...
Страница 114: ...114 ...