首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >smartfoxserver2x是否支持推流?

smartfoxserver2x是否支持推流?
EN

Stack Overflow用户
提问于 2013-07-21 09:03:49
回答 2查看 383关注 0票数 0

这里没有太多的文档,但下面是关于编写服务器端扩展的部分:

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

下面是功能列表:

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

没有提到推送或队列。

有一些宾果游戏使用了smartfox服务器,例如,他们一定是在用它来传球。

使用SFS2x可以做到这一点吗?它是否支持推送通知和理想情况下要推送到一组客户端的项目队列?如果是这样的话,有没有人有源代码或例子?

令人遗憾的是,SFS论坛已经关闭,不能张贴。

EN

回答 2

Stack Overflow用户

发布于 2013-07-27 08:05:04

我用sfs2x开发了一款宾果游戏。我只能给你一个向导,所以我希望它能帮到你。如果我理解这个问题,您希望在不触发命令的请求或事件的情况下发送命令。

创建一个commands类:包路径是相对于src文件夹的。

代码语言:javascript
复制
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‘类,并从你的扩展中调用这个类。我的目标是拥有多个同时运行游戏的房间。我用ConcurrentHashMap追踪了这些游戏:

代码语言:javascript
复制
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类有一个触发抽奖的计时器

代码语言:javascript
复制
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);

    }

在客户端

添加事件监听器

代码语言:javascript
复制
sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);

添加处理程序

代码语言:javascript
复制
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
        }
    }

我试着保持简短,但这并没有发生。我希望我给了你足够的代码来向你展示我是如何实现推送的?在我的“宾果游戏”里。

票数 0
EN

Stack Overflow用户

发布于 2013-07-27 23:17:23

尝试我们的推送连接游戏功能HTML5 sample game walkthrough

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17768140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档