Sending Point-to-point Text and Channel Messages

Sending Point-to-point Text and Channel Messages

Section 1: Integration

Step 1: Prepare the Environment.

Ensure that your development environment meets the following requirements:

OS System Requirements Network Requirements
iOS iOS 7.0 or later HTTP Port 80. TCP Port 8181.

Note

Use a real Android device or an emulator.

Step 2: Download the Agora Signaling SDK.

Download the Agora Signaling SDK.

Step 3: Unpack the downloaded SDK and save the files in the libs folder to the corresponding folder of your project.

Note

The Agora Signaling SDK uses libc++ (LLVM) by default. To use libstdc++ (GNU), contact support@agora.io. The library provided by the Agora Signaling SDK is a FAT image,which includes versions for the 32/64-bit emulator and the 32/64-bit real device.

Step 4: Add the required framework.

To add the required library:

  1. Choose the current Target.
  2. In Xcode, set the search path of the framework to the Agora SDK lib folder.
  3. Click Building Phases to add the required library.
../_images/xcode_4.png
  1. Click Link Binary with Libraries > + to add the following libraries.

    ../_images/ios_project_3.png

Note

AgoraSigKit.framework is in the libs folder of your project. Click + > Add Other… to enter the project folder to add the libraries.

../_images/xcode_5.png

Step 5: Import the header file.

  1. Create a new bridging header file. For example: agora-Bridging-Header.h.
  2. Add the following header files to the bridging header file before using the Agora Signaling SDK.
#import <CommonCrypto/CommonDigest.h>
  1. Set the bridging header file to Objective-C Bridging Header:
    1. Choose the current Target.
    2. Choose Building Settings > Swift Compiler-General, and set Objective-C Bridging Header to <targetname>/agora-Bridging-Header.h.

Step 6: Miscellaneous settings.

  1. Choose the current Target.

  2. Disable bitcode:

    ../_images/bitcode.png

Step 7: Get an App ID and an App Certificate.

For more information see Get a Signaling Key.

Step 8: Generate a token.

Agora recommends generating a token on the server. For more information about generating a token, see: https://github.com/AgoraIO/AgoraDynamicKey/.

You are now all set and can call the APIs provided by the Agora Signaling SDK.

Section 2: Scenario-based Code Snippets

This section provides code snippets related to the following two scenarios:

  • Sending point-to-point messages
  • Sending channel messages

Sending a Point-to-point Message

Sender

/* Initialize the SDK. */
let AgoraSignalKit : AgoraAPI = AgoraAPI.getInstanceWithoutMedia(KeyCenter.AppId)
/* Login to the Agora Signaling system. */
AgoraSignalKit.login2(KeyCenter.AppId, account: account, token: token, uid: 0, deviceID: nil, retry_time_in_s: 60, retry_count: 5)
/* Set the onLoginSuccess callback. */
AgoraSignalKit.onLoginSuccess = { (uid,fd) -> () in
      /* Your code. */
}
/* Set the onLoginFailed callback. */
AgoraSignalKit.onLoginFailed = {(ecode) -> () in
      /* Your code. */
}
/* Send a point-to-point message. */
AgoraSignalKit.messageInstantSend(account, uid: 0, msg: message, msgID: msgID)
/* Set the onMessageSendSuccess callback. */
AgoraSignalKit.onMessageSendSuccess = { (messageID) -> () in
     /* Your code. */
}
/* Set the onMessageSendError callback. */
AgoraSignalKit.onMessageSendError = { (messageID, ecode) -> () in
     /* Your code. */
}
/* Logout of the Agora Signaling system. */
AgoraSignalKit.logout()

Receiver

/* Initialize the Signaling SDK. */
let AgoraSignalKit : AgoraAPI = AgoraAPI.getInstanceWithoutMedia(KeyCenter.AppId)
/* Login to the Agora Signaling system. */
AgoraSignalKit.login2(KeyCenter.AppId, account: account, token: token, uid: 0, deviceID: nil, retry_time_in_s: 60, retry_count: 5)
/* Set the onLoginSuccess callback. */
AgoraSignalKit.onLoginSuccess = { (uid,fd) -> () in
      /* Your code. */
}
/* Set the onLoginFailed callback. */
AgoraSignalKit.onLoginFailed = {(ecode) -> () in
     /* Your code. */
}
 /* Set the onMessageInstantReceive callback. */
 AgoraSignalKit.onMessageInstantReceive = { (account, uid, msg) -> () in
      /* Your code. */
}
/* Logout of the Agora Signaling system. */
AgoraSignalKit.logout()

Sending a Channel Message

Sender

/* Initialize the Signaling SDK. */
let AgoraSignalKit : AgoraAPI = AgoraAPI.getInstanceWithoutMedia(KeyCenter.AppId)
/* Login to the Agora Signaling system. */
AgoraSignalKit.login2(KeyCenter.AppId, account: account, token: token, uid: 0, deviceID: nil, retry_time_in_s: 60, retry_count: 5)
/* Set the onLoginSuccess callback. */
AgoraSignalKit.onLoginSuccess = { (uid,fd) -> () in
      /* Your code. */
}
/* Set the onLoginFailed callback. */
AgoraSignalKit.onLoginFailed = {(ecode) -> () in
      /* Your code. */
}
/* Join a channel. */
AgoraSignalKit.channelJoin(channelName)
/* Set the onChannelJoined callback. */
AgoraSignalKit.onChannelJoined = { (channelID) -> () in
     /* Your code. */
}
/* Set the onChannelJoinFailed callback. */
AgoraSignalKit.onChannelJoinFailed = { (channelID, ecode) -> () in
      /* Your code. */
}
/* Send a channel message. */
AgoraSignalKit.messageChannelSend(channelName, msg: message, msgID: msgID)
/* Set the onMessageSendSuccess callback. */
AgoraSignalKit.onMessageSendSuccess = { (messageID) -> () in
      /* Your code. */
}
/* Set the onMessageSendError callback. */
AgoraSignalKit.onMessageSendError = { (messageID, ecode) -> () in
      /* Your code. */
}
/* Leave the channel. */
AgoraSignalKit.channelLeave(channelName)
/* Set the onChannelLeaved callback. */
AgoraSignalKit.onChannelLeaved = { (channelID, ecode) -> () in
      /* Your code. */
}
/* Logout of the Agora Signaling system. */
AgoraSignalKit.logout()

Receiver

/* Initialize the Signaling SDK. */
let AgoraSignalKit : AgoraAPI = AgoraAPI.getInstanceWithoutMedia(KeyCenter.AppId)
/* Login to the Agora Signaling system. */
AgoraSignalKit.login2(KeyCenter.AppId, account: account, token: token, uid: 0, deviceID: nil, retry_time_in_s: 60, retry_count: 5)
/* Set the onLoginSuccess callback. */
AgoraSignalKit.onLoginSuccess = { (uid,fd) -> () in
      /* Your code. */
}
/* Set the onLoginFailed callback. */
AgoraSignalKit.onLoginFailed = {(ecode) -> () in
      /* Your code. */
}
/* Join a channel. */
AgoraSignalKit.channelJoin(channelName)
/* Set the onChannelJoined callback. */
AgoraSignalKit.onChannelJoined = { (channelID) -> () in
     /* Your code. */
}
/* Set the onChannelJoinFailed callback. */
AgoraSignalKit.onChannelJoinFailed = { (channelID, ecode) -> () in
      /* Your code. */
}
/* Set the onMessageChannelReceive callback. */
AgoraSignalKit.onMessageChannelReceive = { (channelID, account, uid, msg) -> () in
      /* Your code. */
}
/* Leave the channel. */
AgoraSignalKit.channelLeave(channelName)
/* Set the onChannelLeaved callback. */
AgoraSignalKit.onChannelLeaved = { (channelID, ecode) -> () in
      /* Your code. */
}
/* Logout of the Agora Signaling system. */
AgoraSignalKit.logout()