sendResult는 Flex의 Responder 클래스와 짝을 이룹니다.  NetConnection클래스의 call()함수로 서버측 메소드를 호출 할 경우, 서버측에서 클라이언트로 결과를 반환 해야 하는 경우 유용합니다.  클라이언트쪽에서는 Responder클래스를 이용해 응답을 받습니다.


1. sendResult 기본 예제


* Server측 소스

 ...

 public void sendResultTest(IClient client, RequestFunction function, AMFDataList params) {

      sendResult(client, params, "Hello World!");

 }

 ...

   

* Client측 소스

 ...

 nc.call("sendResultTest", new Responder(function(data:Object):void

 {

       Cc.log(data);

 }));

 ...

                 


Tutorial6.fxp

Tutorial6.java

 



2. sendResult 활용 예제


아래 소스는 WOWZA에서 제공해주는 예제입니다. 아~주 유용하고, 여러가지로 활용될 예제라 눈여겨 봐야할 것 같습니다~ 서버측 접속정보(총원, 스트림정보 등)를 알아내는 예제입니다.


[출처] How to get connection counts for server, applications, application instances sent to a Flash application


* Server측 소스

 ...

 public void getServerConnectionCount(IClient client, RequestFunction function, AMFDataList params)

 {

      Server server = Server.getInstance();

      int count = (int)server.getConnectionCounter().getCurrent();

      sendResult(client, params, count);

 }

 

  public void getApplicationConnectionCount(IClient client, RequestFunction function, AMFDataList params)

  {

      IApplication application = client.getApplication();

      int count = (int)application.getConnectionCounter().getCurrent();

      sendResult(client, params, count);

  }

 

  public void getApplicationInstanceConnectionCount(IClient client, RequestFunction function, AMFDataList params)

  {

      IApplicationInstance applicationInstance = client.getAppInstance();

      int count = (int)applicationInstance.getConnectionCounter().getCurrent();

      sendResult(client, params, count);

  }

 

  public void getStreamConnectionCount(IClient client, RequestFunction function, AMFDataList params)

  {

      String streamName = params.getString(PARAM1);

      int count = 0;

      IApplicationInstance applicationInstance = client.getAppInstance();

      List<IMediaStream> streamList = applicationInstance.getPlayStreamsByName(streamName);

      if (streamList != null)

      count = streamList.size();

      sendResult(client, params, count);

  }

 

  public void getStreamClientIds(IClient client, RequestFunction function, AMFDataList params)

  {

      AMFDataArray clientList = new AMFDataArray();

      String streamName = params.getString(PARAM1);

      IApplicationInstance applicationInstance = client.getAppInstance();

      List<IMediaStream> streamList = applicationInstance.getPlayStreamsByName(streamName);

      if (streamList != null)

      {

         Iterator<IMediaStream> iter = streamList.iterator();

         while (iter.hasNext())

         {

             IMediaStream stream = iter.next();

             if (stream == null)

                  continue;

             IClient sclient = stream.getClient();

             if (sclient == null)

                  continue;

             clientList.add(new AMFDataItem(sclient.getClientId()));

         }

      }

      sendResult(client, params, clientList);

 }

 ...

   

* Client측 소스

 ...

 Cc.clear();

 var ncServerResults:Object = new Object()

 ncServerResults.onResult = function(count:Number):void

 {

        Cc.log("server: "+count);

 }

 nc.call("getServerConnectionCount", new Responder(ncServerResults.onResult));

                            

 var ncApplicationResults:Object = new Object()

 ncApplicationResults.onResult = function(count:Number):void

 {  

Cc.log("application: "+count);

 }

 nc.call("getApplicationConnectionCount", new Responder(ncApplicationResults.onResult));

                            

 var ncAppInstanceResults:Object = new Object()

 ncAppInstanceResults.onResult = function(count:Number):void

 {

       Cc.log("appInstance: "+count);

 }

 nc.call("getApplicationInstanceConnectionCount", new Responder(ncAppInstanceResults.onResult));

                          

 var streamName:String = "myStream";

                          

 var ncStreamResults:Object = new Object()

 ncStreamResults.onResult = function(count:Number):void

 {

       Cc.log("stream: "+count);

 }

 nc.call("getStreamConnectionCount", new Responder(ncStreamResults.onResult), streamName);

                          

 var ncStreamListResults:Object = new Object()

 ncStreamListResults.onResult = function(list:Array):void

 {

       Cc.log("clients");

       for(var i:Number=0;i<list.length;i++)

              Cc.log("["+i+"]: "+list[i]);

 }

 nc.call("getStreamClientIds", new Responder(ncStreamListResults.onResult), streamName);

 ...

   

Tutorial7.fxp

Tutorial7.java


저작자 표시 비영리 변경 금지
신고
YOUR COMMENT IS THE CRITICAL SUCCESS FACTOR FOR THE QUALITY OF BLOG POST