我有一个多人项目并使用 SmartFoxServer。我正在编写服务器扩展,但不确定最佳实践是什么。我看到一些奇怪的行为,我不确定这是否是由于我的课程设置方式造成的。在我的扩展中,我有一个这样的主类:
public class MyExtension extends SFSExtension
{
private int numberOfPlayersReady = 0;
@Override
public void init()
{
addRequestHandler("dealingHand",MoveHandler.class );
}
//...some code
public int getNumberOfPlayersReady(){
return numberOfPlayersReady;
}
void setNumberOfPlayersReady(){
numberOfPlayersReady++;
}
}
移动处理器.java:
@Instantiation(InstantiationMode.SINGLE_INSTANCE)
public class MoveHandler extends BaseClientRequestHandler{
@Override
public void handleClientRequest(User user, ISFSObject params)
{
MyExtension gameExt = (MyExtension) getParentExtension();
int currentNumber = gameExt.getNumberOfPlayersReady();
trace("another player is ready " + currentNumber);
if(currentNumber == 3){
//do something
}else{
int newCurrentNumber = (int)currentNumber + 1;
gameExt.setNumberOfPlayersReady();
}
}
}
然后在我的 Unity C# 代码中调用我的服务器扩展代码,如下所示:
SFSObject obj = new SFSObject();
obj.PutInt("playerCheckingInID", sfs.MySelf.Id);
sfs.Send(new ExtensionRequest("dealingHand", obj, sfs.LastJoinedRoom));
但是,当我查看 SmartFoxServer 独立命令行以查看跟踪时,我看到:
"another player is ready 0"
"another player is ready 0"
"another player is ready 0"
"another player is ready 3"
我想我会看到:
"another player is ready 0"
"another player is ready 1"
"another player is ready 2"
"another player is ready 3"
谁能让我知道这里发生了什么?这是否与使用 Single_Instance 类有关?
编辑:或者,这与同步线程有关吗?如果是这样,管理它的最佳方法是什么?这是一款多人回合制游戏。总会有四个玩家。发送到服务器扩展的请求应按照接收顺序进行管理。