tags:

views:

1665

answers:

8

I am wondering how to implement a live chat on a web site using .NET (C#)

And whether it can be linked to your Windows Live Messenger somehow.

A: 

if you are talking about live-support type interface I would go with one of the many 3rd party services/components like cute live support, no sense reinventing the wheel.

if you are doing this just as a hobby project or need actual chat feature you can simulate real time chat with frequent async calls(Every 1 second) that poll the server for new chat messages.

Element
Long polling is probably preferable to pounding the server, even in a hobby project.
Jeffrey Hantin
A: 

Take a look at the documentation here to implement an Live Messenger based chat on your web page. Here's an example of it in action (in french).

Eclipse
A: 

You could also use silverlight for your front-end. This article describes the implementation of a chat using WCF + WPF, this shouldn't be too difficult to adapt to WCF + Silverlight.

Axelle Ziegler
A: 

I agree with Element that there is probably little sense in reinventing the wheel here, but if you're really intending on implementing chat yourself for whatever reason, you probably don't want to poll once per second. Consider instead using a technique like long polling: the client always leaves a pending request to the server open, so the server can simply fire off data and complete the request to deliver an event. This has built-in flow control: further events simply queue at the server until the client reopens the receive request.

Jeffrey Hantin
+3  A: 

This is very easily accomplished with AJAX. I hate to just hand out code, but this is from a book I read last year, that did just what you want.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class GroupChat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageUI();
        RefreshConversation();
    }

    void ManageUI()
    {
        if (GetUserID() == null)
        {
            // if this is the first request, then get the user's ID
            TextBoxMessage.Enabled = false;
            TextBoxConversation.Enabled = false;
            ButtonAddYourMessage.Enabled = false;

            ButtonSubmitID.Enabled = true;
            TextBoxUserID.Enabled = true;
        }
        else
        {
            // if this is the first request, then get the user's ID
            TextBoxMessage.Enabled = true;
            TextBoxConversation.Enabled = true;
            ButtonAddYourMessage.Enabled = true;

            ButtonSubmitID.Enabled = false;
            TextBoxUserID.Enabled = false;
        }
    }


    void RefreshConversation()
    {
        List<string> messages = (List<string>)Cache["Messages"];
        if (messages != null)
        {
            string strConversation = "";

            int nMessages = messages.Count;

            for (int i = nMessages - 1; i >= 0; i--)
            {
                string s;

                s = messages[i];
                strConversation += s;
                strConversation += "\r\n";
            }

            TextBoxConversation.Text =
                strConversation;
        }
    }

    protected void ButtonAddYourMessage_Click(object sender, EventArgs e)
    {
        // Add the message to the conversation...
        if (this.TextBoxMessage.Text.Length > 0)
        {
            List<string> messages = (List<string>)Cache["Messages"];
            if (messages != null)
            {
                this.TextBoxConversation.Text = "";

                string strUserID = GetUserID();

                if (strUserID != null)
                {
                    messages.Add(strUserID +
                        ": " +
                        TextBoxMessage.Text);
                    RefreshConversation();

                    TextBoxMessage.Text = "";
                }
            }
        }
    }
    protected void ButtonSubmitID_Click(object sender, EventArgs e)
    {
        Session["UserID"] =
            TextBoxUserID.Text;
        ManageUI();
    }

    protected string GetUserID()
    {
        string strUserID =
            (string)this.Session["UserID"];

        return strUserID;
    }

}

Rudimentary, but it works. There are 2 textboxes, 2 buttons, and a updatepanel. The messages are held in a List created in Global.asax, and stored in state.

BBetances
+1  A: 

You will love Meebo Rooms. Takes no resources of your own and it just works. I understand some people want to be in control a lot of times, but with the amounts of services out there, it makes sense to just mash them up.

Try it out

Meebo Rooms

NTulip
A: 

Thanks everyone for your helpful comments.

The Live Messenger API is also interesting but we don't want our visitors to have to login to send a message to us.

I don't think Meebo suits us because we're looking for a one-to-one chat, a visitor with one of our operators. Thanks anyway :)

now that i better understand your question, please look at http://www.estara.com and their "click to chat" product which I can presonally recommend.
NTulip
A: 

It's complicated to install and maintain a live chat software. Have a try of Comm100 Live Chat, Zero installation and maintenance. Moreover, it is completely free. :)

http://www.comm100.com/livechat/

Carle