I have developed a bingo game in sfs2x. I can only give you a guide so I hope it helps. If I understand the question, you want to send commands without a request or event triggering the command.
Create a commands class:
the package path is relative to you src folder.
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";
}
I have not included all the commands I used. There is no need to set up event or request handlers because the command is sent from server side timer event.
You will have to write a 'bingogame' class and call that class from you extension. My objective was to have a number of rooms that run games concurrently. I tracked the games using ConcurrentHashMap:
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();
}
}
The BingoGame Class has a timer triggering the draws
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);
}
on the client side
add an event listener
sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
add the handler
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
}
}
I tried to keep this brief but it didn't happen. I hope I've given you enough code to show you how I achieved a push? in my 'BINGO GAME'.