2013-07-21 62 views
0

沒有太多的文件,但在這裏是如何編寫服務器端擴展部分:是否支持smartfoxserver2x推送?

http://docs2x.smartfoxserver.com/AdvancedTopics/server-side-extensions

這裏是功能列表:

http://docs2x.smartfoxserver.com/Overview/sfs2x-features

沒有推提或隊列。

有使用smartfox服務器的賓果遊戲,他們必須使用它來舉例說明球。

這可以使用SFS2x來完成嗎?它是否支持推送通知並理想地將項目隊列推送到一組客戶端?如果是這樣,有沒有人有任何源代碼或例子?

SFS論壇,可悲地關閉,並不公開張貼。

回答

0

我開發了sfs2x中的賓果遊戲。我只能給你一個指導,所以我希望它有幫助。如果我理解了這個問題,您希望發送沒有請求或事件觸發命令的命令。

創建一個命令類: 包路徑是相對於你的src文件夾。

package com.rcras.bingo1; 

public class Commands 

{ 
/** User buys bingo cards */ 
public static final String CMD_BUY_CARDS = "bc"; 
/** Server sends Bingo */ 
public static final String CMD_BINGO = "bo"; 

/** User Calls Bingo */ 
public static final String CMD_CALL_BINGO = "bingo"; 

/** Get the time left till next game */ 
public static final String CMD_GET_TIMER = "get_timer"; 

/** No bingos left --- Game Over */ 
public static final String CMD_GAME_OVER= "gaov"; 

/** Ready to start a game */ 
public static final String CMD_READY = "ready"; 

/** Bingo Draw */ 
public static final String CMD_DRAW = "draw"; 
/** tell the client app the game is starting */ 
public static final String CMD_START = "start"; 
} 

我沒有包括所有我使用的命令。無需設置事件或請求處理程序,因爲該命令是從服務器端計時器事件發送的。

您將不得不編寫一個'bingogame'類並從您的擴展中調用該類。我的目標是有一些同時運行遊戲的房間。

import java.util.concurrent.ConcurrentHashMap; 
import com.rcras.bingo1.Commands; 

public class Bingo1 extends SFSExtension { 
    List<User> DiamondRoomPlayers; 
    Timer timer; 
    private static int min =2; 
    private static int sec = 60; 
    /** Current games */ 
    private ConcurrentHashMap<Integer,BingoGame> games = null; 


public ConcurrentHashMap<Integer, BingoGame> getGames() { 
    return games; 
} 

public void startDiamondGame() 
{ 
    Zone thisZone = this.getParentZone(); 
    Room room2 = thisZone.getRoomByName("Diamond"); 
    BingoGame bingoGame = this.getGames().get(room2.getId()); 
    if(bingoGame == null || (bingoGame != null && bingoGame.isStarted() == false)) 
    { 
      DiamondRoomPlayers=room2.getUserList(); 
      ISFSObject DiamondObj = new SFSObject();  
      send(Commands.CMD_START, DiamondObj, DiamondRoomPlayers); 
      BingoGame DiamondRoomGame = new BingoGame(this, room2); 
      getGames().put(room2.getId(),DiamondRoomGame); 
      DiamondRoomGame.setId(room2.getId()) ; 
      DiamondRoomGame.init(); 
    } 

} 

的BingoGame類有一個定時器觸發在客戶端

平局

public class BingoGame { 
public BingoGame(Bingo1 ext, Room room) 
{ 
    Draw= new int[75]; 
    setPlayers(room.getUserList()); 
    //players = room.getUserList(); 
    thisRoom = room; 
    this.extension=ext; 
//setters 
    this.setFirstBingo(true); 
    this.setStarted(false); 
    this.setSecondBingo(true); 
    this.setFirstPrize(1000); 
    this.setSecondPrize(500); 
} 
public void init() 
{ 
    if(players.size() > 0) 
    { 
     StartBingoGame(); 
    } 
    else 
    { 
     System.out.println("NOT ENOUGH PLAYER IN THE" + thisRoom.getName() + " Room!"); 
    } 
} 
private void StartBingoGame() 
{ 
    timer = new Timer(); 
    timer.schedule(new BingoDrawTask(), 
       0,  //initial delay 
       1*3000); //draw a number every 3 seconds 
    setStarted(true); //setter for started variable 

} 

class BingoDrawTask extends TimerTask { 

    private int BingoNum; 
    private boolean isThere = true; 
    @Override 
    public void run() { 

     isThere = true; 

     Random BingoCall = new Random(); 
     while(isThere == true) 
     { 
     BingoNum = BingoCall.nextInt(75) + 1; 
     System.out.println("Bingo Draw:" + BingoNum); 
     isThere = CheckDrawArray(BingoNum); 
     } 
     Draw[NunbersCalled]=BingoNum; 
     NunbersCalled = NunbersCalled + 1; 
     System.out.println(thisRoom.getName() + " Room: Draw Number: " + NunbersCalled); 
//this should never happen but it's there for testing 
     if (NunbersCalled == 75) 
     { 
      System.out.println("STOP!!!"); 
      StopBingoGame(); 
     } 
     // Empty obj 
     ISFSObject numObj = new SFSObject(); 
     numObj.putInt("cn", NunbersCalled); 
     numObj.putInt("dn", BingoNum); 
     numObj.putUtfString("rn", thisRoom.getName()); 
/*This command 'pushes' the command to a player list named players 
     extension.send(Commands.CMD_DRAW, numObj, players); 
     System.out.println("Send ----" + BingoNum); 

    } 

添加事件偵聽器

sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse); 

補充:我使用的ConcurrentHashMap跟蹤遊戲處理者

private function onExtensionResponse(evt:SFSEvent):void 
    { 
     var obj:SFSObject = evt.params.params as SFSObject; 

     if(evt.params.cmd == "draw") 
     { 
      //Handle draw - mostly handled by BingoCard class 
     } 
    } 

我試圖保持這個簡短但它沒有發生。我希望我已經給了你足夠的代碼來向你展示我是如何實現推動的?在我的'BINGO GAME'中。