Export (0) Print
Expand All

Managing Conversations

Gg276544.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.

You can give users the ability to have conversations with members of their Windows Live Messenger contact list.

Through use of the Conversation Control class and the Conversation List Control, you can have the Messenger Connect UI Controls manage conversations for you.

However, if you want to use the Windows Live Messenger Library instead, you need to incorporate the following functionality into your application.

To create an instance of Microsoft.Live.Messenger.Conversation
  • Call the create method of ConversationCollection and pass it the instant messaging address of the user with whom the conversation will be initiated.

    var imaddress = contact.get_currentAddress();
    _conv = user.get_conversations().create(imaddress);
    
To send a message
  1. Create an object of class TextMessage.

    var message = new Microsoft.Live.Messenger.TextMessage("A message.", null);
    
  2. Call the S method of the conversation object.

    _conv.sendMessage(message, null);
    
To subscribe to Microsoft.Live.Messenger.MessageReceivedEventHandler
  • Call the add_messageReceived function and pass it the conversation object.

    _conv.add_messageReceived(recvMsg);
    
To listen for incoming messages by using the User.Conversations property
  1. Create an event handler delegate for the CollectionChanged event.

    user.get_conversations().add_propertyChanged(conversation_collectionChanged);
    
  2. Add an event handler function by using the name provided in the previous step. (conversation_collectionChanged).

    This function is called by the Windows Live Messenger Library when the list of conversations changes. In this example, the roster list for each conversation appears.

    function conversation_collectionChanged(sender,e) 
    {
        for (var i = 0; i < user.get_conversations().get_count(); i++)
        {
            var c = user.get_conversations().get_item(i);
                    
            if (c.get_closed())
            {
                continue;
            }
                    
            convLink(c, i);
        }
    }
    
    function convLink(c, item) 
    {
        var roster = c.get_roster();
        var names = [];
        for (var i = 0; i < roster.get_count(); i++)
        {
            var address = roster.get_item(i);
            var dispName = address.get_presence().get_displayName() || address.get_address();
            names.push(dispName);
        }
        var allNames = names.join(", ");
        var nameList = document.createElement("span");
        nameList.appendChild(document.createTextNode(allNames + " "));
    }
    
To handle incoming messages
  1. Create a new function that takes the sender object and Microsoft.Live.Messenger.MessageReceivedEventArgs object as arguments.

    recvMsg = function (sender, e) 
    {
    }
    
  2. Get the message property of the Microsoft.Live.Messenger.MessageReceivedEventArgs object and then use the property value to get the message text, sender information, and time stamp.

    var message = e.get_message();
    var sender = message.get_sender().get_presence().get_displayName()
    var msgText = message.get_text();
    var timestamp = message.get_timestamp();
    

Description

The following example demonstrates functions for creating a conversation, sending a message, handling incoming messages, and updating the conversation window.

Code

// Get the User instance
var messengerContext = wl.app.get_messenger_Context();
var user = messengerContext.get_user();

// Create a new conversation with the selected contact.
_conv = user.get_conversations().create(contact.get_currentAddress());
// Subscribe to Microsoft.Live.Messenger.MessageReceivedEventHandler.
_conv.add_messageReceived(recvMsg);
// Update the page with the name of the user with whom the chat is 
// occurring.
document.getElementById('contactLabel').innerText = 
    'Now chatting with: ' + contact.get_currentAddress().get_address();

// Send a message when the Send button is clicked.
function sendMsg() 
{
    // The message text is taken from the text input 'txtMessage'.
    var txtMessage = document.getElementById('txtMessage');
    var messageText = txtMessage.value;
    // Create a new Microsoft.Live.Messenger.Message.
    var message = new Microsoft.Live.Messenger.TextMessage(messageText,
                  null);

    if (user) 
    {
        _conv.sendMessage(message, null);
    }

    // Call the displayMsg function to update the window.
    displayMsg(message);
    // Clear the text box and scroll to the end.
    txtMessage.value = '';
    txtMessage.focus();
}

// Handle incoming messages.
function recvMsg(sender, e) 
{
    switch (e.get_message().get_type())
    {
        case Microsoft.Live.Messenger.MessageType.textMessage:
            var message = e.get_message();
            displayMsg(message);
            break;
        default:
            break;
    }               
}

// Update the conversation window with outgoing and incoming 
// message text.
function displayMsg(message) 
{
    var elMsg = message.createTextElement();
    var txtConv = document.getElementById("txtConv");
            
    var sender = message.get_sender();
    var dispName = sender.get_presence().get_displayName() 
                                         || sender.get_address();
            
    txtConv.appendChild(document.createTextNode(dispName + 
                                                " says: "));
    txtConv.appendChild(elMsg);
    txtConv.appendChild(document.createElement("br"));
}
Show:
© 2015 Microsoft