The SignalingSession class enables an application to exchange messages with the remote participant. An application can send a message synchronously by calling SendMessage. To send a message asynchronously, the application can call BeginSendMessage and EndSendMessage. An application can receive a message by implementing an event handler for the MessageReceived event, and registering the event handler.
Sending Messages Synchronously
The programming pattern for sending a message synchronously is illustrated by the following code example.
Copy Code | |
---|---|
// Send a Message synchronously ContentType contentType = new ContentType("text/plain", "utf-8"); byte[] body = Encoding.UTF8.GetBytes("Hello"); try { session.SendMessage(MessageType.Message, contentType, body); } catch (InvalidOperationException exp) { // Handle exception in sending the message. } |
Sending Messages Asynchronously
The programming pattern for sending a message asynchronously is illustrated by the following code example. A notable difference between this pattern and that for sending messages synchronously is that the call to BeginSendMessage includes a callback argument, messageCallback. When BeginSendMessage returns, the callback (SendMessage_Callback) begins execution, causing EndSendMessage to execute.
The type of message to send is determined by the first argument in the call to BeginSendMessage, which is of type Message. Other types of messages can be sent, including BeNotify, Info, Options, and Service. For more information, see MessageType.
Copy Code | |
---|---|
// Send a message asynchronously ContentType contentType = new ContentType("text/plain", "utf-8"); byte[] body = Encoding.UTF8.GetBytes("Hello"); AsyncCallback messageCallback = new AsyncCallback(this.SendMessage_Completed); try { session.BeginSendMessage(MessageType.Message, contentType, body, messageCallback, session); } catch (RealTimeException exp) { // Handle exception in sending the message. } void SendMessage_Completed(IAsyncResult asyncResult) { SignalingSession session = asyncResult.AsyncState as SignalingSession; try { session.EndSendMessage(asyncResult); } catch (FailureResponseException exp) { // Handle a failure response Console.WriteLine("Failure response for {0}: {1}", session.RemoteParticipant.Uri, exp.ToString()); } catch (OperationTimeoutException exp) { // Operation timed out Console.WriteLine("Operation timed out for {0}: {1}", session.RemoteParticipant.Uri, exp.ToString()); } catch (RealTimeException exp) { // Handle exception. Console.WriteLine("Invite Failure for {0}: {1}", session.RemoteParticipant.Uri, exp.ToString()); } } |
Receiving Messages
The programming pattern for receiving a message in a signaling session is illustrated by the following code example. At the beginning of the example, the developer-implemented Message_Received event handler is registered to handle the MessageReceived event. When a message is received, the MessageReceived event is raised, and Message_Received begins execution.
In this example, the event handler checks that the MediaType and CharSet properties are set to "text/plain" and "utf-8," respectively. If so, the call to GetBody retrieves the body of the message. For a message whose MessageType is Info, "Info received:" is displayed, followed by the contents of the message body. For a message whose MessageType is Message, "Message received:" is displayed, followed also by the contents of the message body.
Copy Code | |
---|---|
// Register an event handler for the MessageReceived event. session.MessageReceived += this.Message_Received; // Handle message and info received event void Message_Received(object sender, MessageEventArgs e) { SignalingSession session = sender as SignalingSession; MessageType messageType = e.MessageType; if ((e.ContentType.MediaType.CompareTo("text/plain") == 0) && (e.ContentType.CharSet.CompareTo("utf-8") == 0)) { string message = Encoding.UTF8.GetString(e.GetBody() ); if (messageType.MessageType == MessageType.Info) { Console.WriteLine("Info Received: {1}", message); } else if (messageType.MessageType == MessageType.Message) { Console.WriteLine("Message Received: {1}", message); } else { // Handle other MessageType values } else { // Handle cases where MediaType != "text/plain" and CharSet != "utf-8", if necessary } } |