Click to Rate and Give Feedback
MSDN
MSDN Library
Live Services SDK
 How to: Manage Conversations

  Switch on low bandwidth view
How to: Manage Conversations

You can give users the ability to have a conversation with any member of their Windows Live™ Messenger contact list.

By using the Conversation Control and the Conversation List Control, you do not need to manage conversations. The UI Controls will manage conversations for you.

If you would like to use the Windows Live Messenger Library instead, you will need to incorporate the following functionality into your application.

  • Create an instance of Microsoft.Live.Messenger.Conversation.
  • Subscribe to Microsoft.Live.Messenger.MessageReceivedEventHandler.
  • Create a function to send messages.
  • Create a function to handle incoming messages.
To create an instance of Microsoft.Live.Messenger.Conversation
  • Call the create() method on Microsoft.Live.Messenger.Conversation by passing the IMAddress for 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 instance of Microsoft.Live.Messenger.TextMessage.

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

    _conv.sendMessage(message, null);
To subscribe to Microsoft.Live.Messenger.MessageReceivedEventHandler
  • Call the add_messageReceived accessor function on 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 same 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 as input parameters.

    recvMsg = function (sender, e) 
    {
    }
  2. Get the message object and use it 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 code example is an excerpt from the sample HelloWorld application that demonstrates functions for creating a conversation, sending a message, handling incoming messages, and updating the conversation window.

Code

// 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"));
}
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker