Navigation:  Communication Libary >

Invoke Server Methods

Previous pageReturn to chapter overviewNext page
Show/Hide Hidden Text

The library supports remote method invocation via RTMP. The methods the client application invokes through the library must be available in the remote application the client is connected to (see the Application Name parameter in Creating RTMP Connection). The client-side API for invoking the methods is the same regardless of the server the client connects to. However, the mechanism for exposing server-side methods varies between available servers. See the Supported Servers section for the details on exposing server-side methods to the clients.

 

Remote method invocation is available through the invoke method in the RTMPClient class. A connection must be established before the client can call a server-side method. The example below demonstrates an invocation of the echoInt method accepting an argument of type NSNumber. The corresponding server-side type will vary depending on a specific server-side implementation. Various implementations of the method are also provided below.

 

Objective-C Client:

-(void)echoInt {
 // set call parameters
 NSMutableArray *args = [NSMutableArray array];
 NSString *method = [NSString stringWithString:@"echoInt"];
 [args addObject:[NSNumber numberWithInt:12]];        
 
 // send invoke
 [rtmpClientInstance invoke:method withArgs:args];
}

 

hmtoggle_plus1echoInt with WebORB for .NET

using System;
using Weborb.Messaging.Server.Adapter;
 
namespace Weborb.Examples.ClientInvoke
{
 public class AppHandler : ApplicationAdapter
 {
   public int echoInt( int i )
   {
     return i;
   }
 }
}

 

hmtoggle_plus1echoInt with WebORB for Java

package examples.weborb;
 
import weborb.messaging.WebORBApplication;
 
public class AppHandler extends WebORBApplication
{
  public int echoInt( int i )
  {
    return i;
  }
}

 

hmtoggle_plus1echoInt with Wowza Media Server

package examples.weborb.ios;
 
import com.wowza.wms.amf.*;
import com.wowza.wms.client.IClient;
import com.wowza.wms.module.ModuleBase;
import com.wowza.wms.request.RequestFunction;
 
public class MyWowzaModule extends ModuleBase
{
 public void echoInt( IClient client, RequestFunction function, AMFDataList params )
 {
   int value = getParamInt( params, PARAM1 );
   sendResult( client, params, value );
 }
}

 

hmtoggle_plus1echoInt with Flash Media Server

Client.prototype.echoInt = function( intValue )
{
 trace( "received invocation of enchoInt with arg value - " + intValue );
 return intValue;
}

 

For instructions about deploying server-side code see the Supported Servers section.

 

Result Handling

When the invoked server-side method returns a value, the library delivers it via the IPendingServiceCallback protocol inherited through IRTMPClientDelegate. The implementation of the protocol is the resultReceived method. Consider the following implementation of the method:

-(void)resultReceived:(id <IServiceCall>)call {
 
   int status = [call getStatus];
 
   if (status != 0x01 ) // this call is not a server response (0x01 is used to differentiate this from a data push)
       return;
 
   NSString *method = [call getServiceMethodName];
   NSArray *args = [call getArguments];
 
   if (args.count)
       NSLog(@"resultReceived <---- status=%d, method='%@', return value=%@\n", status, method, [args objectAtIndex:0]);
   else
       NSLog(@"resultReceived <---- status=%d, method='%@',, no return value - void = 0\n", status, method);
}

Additionally, the library API provides more flexible approaches to handle a server side method invocation result. The AsynClass interface provides an object that is used in the responder parameter of the invoke method to handle a result from the server related to the success of the specific invocation. Consider the following implementation of the method:

 

-(void)echoInt {
 // set call parameters
 NSMutableArray *args = [NSMutableArray array];
 NSString *method = [NSString stringWithString:@"echoInt"];
 [args addObject:[NSNumber numberWithInt:12]];        
 
 // send invoke
 [rtmpClientInstance invoke:method withArgs:args responder:[AsynCall call:self method:@selector(onResult:)]];
}

-(void)onResult:(id <IServiceCall>)call
{
   NSString *method = [call getServiceMethodName];
   NSArray *args = [call getArguments];
 
   if (args.count)
       NSLog(@"resultReceived <---- status=%d, method='%@', return value=%@\n", status, method, [args objectAtIndex:0]);
   else
       NSLog(@"resultReceived <---- status=%d, method='%@',, no return value - void = 0\n", status, method);
}

 

In general, a custom interface to handle server invocation results may be created and has to implement the IPendingServiceCallback protocol

 

For additional information see the API documentation for the AsynClass interface as well as IPendingServiceCallback and IServiceCall protocols.