An application can start to send messages in page mode once a RealTimeEndpoint is instantiated and registered if the endpoint is of the SipEndpoint type. The application on the sending side can send a message synchronously (SendMessage) or asynchronously (BeginSendMessage and EndSendMessage). Page-mode messages are delivered only when the recipient endpoint is online and has subscribed to the MessageReceived event.

Sending Page-Mode Messages Synchronously

The following code example shows how to send a simple text message synchronously in page mode. It is assumed that the endpoint is properly created and registered, if it is registration-based.

Copy Code
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere
RealTimeAddress target = new RealTimeAddress("sip:jayanama@contoso.com");
string msg = "Hi!";
ContentType contentType = new ContentType("text/plain; charset=UTF-8");
byte[] msgBody = Encoding.UTF8.GetBytes(msg);
try
{
  endpoint.SendMessage(MessageType.Message,
					 target,
					 contentType,
					 msgBody);
}

catch (RealTimeException e)
{
  // Handle exception.
}

Sending Page-Mode Messages Asynchronously

The asynchronous call to send a page-mode message uses the BeginSendMessage EndSendMessage pattern adopted in the .NET Framework. The pattern requires an application to supply a callback function to be invoked before the asynchronous operation finishes.

Copy Code
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere
RealTimeAddress target = new RealTimeAddress("sip:jayanama@contoso.com");
string msg = "Hi!";
ContentType contentType = new ContentType("text/plain; charset=UTF-8");
byte[] msgBody = Encoding.UTF8.GetBytes(msg);
try
{
  endpoint.BeginSendMessage(MessageType.Message,
							target,
							contentType,
							msgBody,
							CompleteSendMessage,
							endpoint);
}

catch (RealTimeException e)
{
  // Handle exception here.
}

// Async callback function to end the BeginSendMessage operation.
void CompleteSendMessage(AsyncCallback result)
{
  RealTimeEndpoint ep = ar.AsyncState as RealTimeEndpoint;
  SipResponseData data;
  try
  {
	data = ep.EndSendMessage(ar);
  }

  catch (FailureResponseException exp)
  {
	// Handle a failure response
	Console.WriteLine("Failure response for {0}: {1}",
					 ep.Uri,
					 exp.ToString());
  }

  catch (OperationTimeoutException exp)
  {
	// Operation timed out
	Console.WriteLine("Operation timed out for {0}: {1}",
					 ep.Uri,
					 exp.ToString());
  }

  catch (RealTimeException exp)
  {
	// Handle exception.
	Console.WriteLine("Invite Failure for {0}: {1}", 
					 ep.Uri,
					 exp.ToString());
  }
}

Receiving Page-Mode Messages

Subscribing to the event amounts to implementing a MessageReceived event handler and registering it with a RealTimeEndpoint instance. The following is a simple code example that illustrates this process.

Copy Code
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere

endpoint.MessageReceived += pageMode_MessageReceived;

void pageMode_MessageReceived(object sender, MessageReceivedEventArgs e)
{
  if ((e.ContentType.MediaType.CompareTo("text/plain") == 0) &&
	(e.ContentType.CharSet.CompareTo("utf-8") == 0) &&
	(e.HasTextBody == true))
  {
	string msg = Encoding.UTF8.GetString(e.GetBody());
	Console.WriteLine(msg);
  }

  else 
  {
	// Handle other media types and other character encoding types
  }
}

See Also