MessageReceivedCallback Delegate

Ff749528.note(en-us,MSDN.10).gifNote:
Current information about Live Connect is now available in the Windows Live Developer Center. The information in the following sections is provided for legacy purposes only.

Represents the method that is called when a conversation message is received.

Script:  http://js.live.net/4.1/loader.js
Feature:  Microsoft.Live
Namespace:  Microsoft.Live
 

function messageReceivedCallback(cid, message) {

}

Parameters

cid
Type: String

The CID of the sender.

message
Type: Message

The received object of class Message.

A MessageReceivedCallback delegate can be specified when you use the Windows Live Messenger communication and sharing services. In this scenario, you get an instance of the IMessengerContext interface from the messengerContext property of the class App instance. You next pass the MessageReceivedCallback delegate as an argument to the onMessageReceived method of the IMessengerContext instance.

The MessageReceivedCallback delegate is different from the MessageReceivedEventHandler delegate in that the latter takes only one argument—an object of class MessageReceivedEventArgs—and is defined to handle an event of the Conversation control.

The following examples demonstrate how to use the MessageReceivedCallback delegate. In this example, the callback handler, invoked when a message is received, is defined in the application-loaded event handler. Additionally, the MessageReceivedEventHandler delegate is implemented to show the two approaches in comparison.

The first example shows the markup by using a contact list and a conversation control.

<wl:contact-list onselected="onContactSelectedCallback">
</wl:contact-list>
<wl:conversation id="ConversationControl" 
                 onMessageReceived="onMessageReceivedCallback_Declarative">
</wl:conversation>        

The second example shows JavaScript code that supports the markup and defines the MessageReceivedCallback delegate. If the received message is not from a user that is currently in a conversation, you can create a new conversation or take another custom action.

var messengerContext;
function appLoaded(applicationLoadCompletedEventArgs) {
    messengerContext = Microsoft.Live.App.get_messengerContext();
    messengerContext.onMessageReceived(onMessageReceivedCallback);
}
// Handler defined using MessageReceivedCallback
function onMessageReceivedCallback(cid, message) {
    // check if conversation window open
    var conversation = findMatch(cid);
    if (conversation === undefined) {
        // Create a new conversation control and add to page.
        alert('Create new conversation window for incoming message?');
    }
    else {
        alert("handler1: " + message.get_text());
    }
}
function findMatch(cidMatch) {
    var components = Sys.Application.getComponents();
    for (var i = 0; i < components.length; i++) {
        if (components[i].get_id() === "ConversationControl") {
            var cid = components[i].get_cid();
            if (cidMatch === cid) {
                return components[i];
            }
        }
    }
}
// Handler defined on conversation control.
function onMessageReceivedCallback_Declarative(messageReceivedEventArgs) {
    var message = messageReceivedEventArgs.get_message();  // Microsoft.Live.Messenger.Message 
    alert("handler2: " + message.get_text());
}
// Handler called when user selects contact.
function onContactSelectedCallback(contactSelectedEventArgs) {
    var cid = contactSelectedEventArgs.get_contact().get_cid();
    var conversationControl = $find('ConversationControl');
    conversationControl.set_cid(cid);
}

Windows Live Messenger Connect

Supported in: 4.1

Internet Explorer 8.0, Internet Explorer 7.0, Internet Explorer 6.0, Firefox 3.6, Firefox 3.5, Firefox 3.0, Firefox 2.0, Firefox 1.5, Chrome 4.0, Chrome 3.0, Chrome 2.0, Chrome 1.0, Safari 4.0, Safari 3.0

Show: