Click here to Skip to main content
15,867,594 members
Articles / Web Development / ASP.NET
Article

Chat Application using Web services in C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (38 votes)
15 May 2008CPOL5 min read 382.2K   41.8K   139   91
This is a cool chat application created in DotNet using web services having all the functionalities.

Introduction

All of us are more or less using many of the chat applications available in the market everyday but still sometimes wonder of how it has been developed. This is a simple private chat application developed in DotNet using a web service.

The application (EasyTalk, I named it) targets mainly the beginners who still sometimes fear of using the web service. The audience will get a taste of how web servcie can be easily developed and can be used in a chatting application.

Background

The chat client is developed just like any other chat application using Windows form and the chat service is developed using the Dot Net web service residing in the web server.

Zip File Contents

The above attached zip file contains the following projects:

1. Chat Service - This is the web service to be deployed in any web server.

2. Chat Client - The is the chat application through which the user will chat.

3. Chat Setup - The distributable setup package.

4. Chat Testing - To is to test the web service.

Using the code

ChatService.asmx (Web Service)

The ChatService.asmx in the zip file represents the name of the web service in which you will see lot of web methods that will be called by the clients using the application.

[WebService(Namespace = "<a href="%22%22http://tempuri.org/%22%22">http://tempuri.org/</a>")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ChatService : System.Web.Services.WebService
{
    static protected ArrayList arrUsers = new ArrayList();
    static protected ArrayList arrMessage = new ArrayList(); 
  
    public ChatService () { 
    
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

Remember these methods are always 'public', otherwise they won't be available for the outside world. You can only set a public method as a web method, otherwise they will be treated as private methods to the web service.

Let us walk through some of these methods:

[WebMethod]
public void AddUser(string strUser)
{ 
    arrUsers.Add(strUser); 
} 

The AddUser() method will add the user who has logged in the ArrayList being maintained in the web service. By this way the online user list is always updated and will give you the list of users in the room at any point of time.

[WebMethod]
public string GetUsers() 
{
    string strUser = string.Empty; 
    for (int i = 0; i < arrUsers.Count; i++)
    {
        strUser = strUser + arrUsers[i].ToString() + "|";
    }
    return strUser;
}

The GetUser() method will retrieve the list of users who have logged in the application. This list can be easily available from the Arraylist at any time.

[WebMethod]
public void RemoveUser(string strUser)
{
    for (int i = 0; i < arrUsers.Count; i++)
    {
        if(arrUsers[i].ToString() == strUser)
            arrUsers.RemoveAt(i);
    }
}

The RemoveUser() method will do the opposite. It will take out the required name out of the Arraylist whenever the user logout from the application.

[WebMethod]
public void SendMessage(string strFromUser, string strToUser, string strMess)
{
    arrMessage.Add(strToUser + ":" + strFromUser + ":" + strMess); 
}

The SendMessage() method will concatinate the strings (the username to which the message has been sent, the username who has sent the message and the actual message) coming as parameters and will add the new string to another ArrayList. This ArrayList will simply hold all the messages coming from diferent clients intended for different users.

[WebMethod]
public string ReceiveMessage(string strUser)
{
    string strMess = string.Empty;
    for (int i = 0; i < arrMessage.Count; i++)
    {
        string[] strTo = arrMessage[i].ToString().Split(':');
        if (strTo[0].ToString() == strUser)
        {
            for (int j = 1; j < strTo.Length; j++)
            {
                strMess = strMess + strTo[j] + ":";
            }
            arrMessage.RemoveAt(i);
            break;
        } 
    }
    return strMess;
}

The ReceiveMessage() method is doing all the tricks. It will filter the messages from the Arraylist and will send each message to its actual recipient.

1. It is taking out the messages one by one from the Arraylist.

2. It is comparing the 'ToUser' name attached with the message with the username from whom the method is requested.

3. If the names are same means the message is intended for that recipient only. It will return the message to that user and remove the message from the arrayList once it is transfered.

ChatClient (Client Application)

Chat User List Window

The Chat Client is developed using the Windows Forms. You will see two forms by the name Form1 for showing the online user list and PrivateMessage for the chat window.

In this solution we have added the web service as a web reference to this project. So whenever you add the web service that has been deployed in any of your server, the client application will register that path to the server.

The Form1 is the interface that is populating the list of online users logged into the EasyTalk aplication. There is no login form in this application as the username is automatically assigned from the windows login name. The same name will get displayed in the list which on click will open a one to one conversation window to chat with that person. There is a timer attached with this form that will call the GetUser() web method in the web service every 2 seconds to update the list of online users.

chatlist.jpg

Extra Functionalities:

1. Whenever any user logs into the application, a notification window will get popup in all the online users machine from the right corner of the screen to show the name of the new online user. [screenshot below]

NotifyUser.JPG

2. Whenever the user minimises the chat window, the new messeges will start coming to that user in a popup window like the one above from the right corner of the screen like that in gtalk.

3. If the user wishes not to get the messeges in apopup window he can select the checkbox at the bottom of the main window named "Stop Messege Alert service".

Chat Window

The other form is the privateMessage that will allow the user to talk to the other person. There is also a timer attached with this form that will call the ReceiveMessage() method to check whether any new message has arrived from the other side. So if there are 3 windows open for an user, the respective window will look for it's own message through the timer.

Private Message

In the application, the notifyIcon control is also used so that the user can keep the application running in the system tray. There is a menu associated with the application through which the

One catchy thing that has been used in this application is to blink the chat window whenever any new message comes. The blinking will go on till the user clicks the window to read the message. This is done using Windows API.

[DllImport("user32.dll")]
static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);

Configuration Window

There has been a configuration window to change the server name/ip address. If the web service is deployed in a diferent chat server, only the new ip addreess needs to be supplied to the application through the configuration window by the users.

Points of Interest

This is just one way of creating a very simple chat application using a web service. You can create this application using a different approach. You can further procced with this application by creating more functionalities like Public Chatting, Conferences, etc.

Happy Chatting!!!!!!

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
He has a total experience of more than 6 years in programming on different technologies. Currently working as a Systems Analyst.

Comments and Discussions

 
GeneralWebservice not working Pin
adam parnell14-May-09 7:00
adam parnell14-May-09 7:00 
Questionhow setup this program? Pin
qomcse22-Apr-09 5:04
qomcse22-Apr-09 5:04 
AnswerRe: how setup this program? Pin
Kennie_n20008-Dec-09 9:28
Kennie_n20008-Dec-09 9:28 
QuestionProblem with using this Chat Application Pin
malli.reddy.mca16-Apr-09 2:12
malli.reddy.mca16-Apr-09 2:12 
QuestionHow does the data stored in the arraylist declared in the class persist? Pin
rickysundrani13-Jan-09 19:04
rickysundrani13-Jan-09 19:04 
GeneralShutdown problem Pin
Md Sagor Ali11-Jan-09 19:34
Md Sagor Ali11-Jan-09 19:34 
GeneralRe: Shutdown problem Pin
ronaldovn7-Mar-10 19:58
ronaldovn7-Mar-10 19:58 
GeneralSmall Problem (need help..) Pin
pinna_hari9-Dec-08 3:46
pinna_hari9-Dec-08 3:46 
Frown | :(
Hello,

When i am runnig this application i am getting below error

Error in connecting to the chat server.\rPlease check the server configuration setting

is it run in personel system or not?

please tell me how to deploy webservice

waiting for your reply...thanks in advance...

thanks Cry | :((

Pinna

GeneralRe: Small Problem (need help..) Pin
Surajit Paul9-Dec-08 3:55
Surajit Paul9-Dec-08 3:55 
GeneralRe: Small Problem (need help..) Pin
pinna_hari10-Dec-08 18:35
pinna_hari10-Dec-08 18:35 
Generalweb based chat application Pin
schandrashekhar7-Nov-08 21:18
schandrashekhar7-Nov-08 21:18 
Questionhow can i use this for web based chat application Pin
chandrashekharsani23-Oct-08 21:52
chandrashekharsani23-Oct-08 21:52 
GeneralGood work, and I have some advices! Pin
Highflyer14-Sep-08 10:45
Highflyer14-Sep-08 10:45 
QuestionHow to configure the chat application in windows xp Pin
Dhanasekaran R28-Aug-08 1:47
Dhanasekaran R28-Aug-08 1:47 
Questionhow to create chat service Pin
desigank27-Aug-08 0:54
desigank27-Aug-08 0:54 
Questionhow to maintain the database Pin
Pandian60215-Jul-08 23:59
Pandian60215-Jul-08 23:59 
AnswerRe: how to maintain the database Pin
Surajit Paul24-Jul-08 9:22
Surajit Paul24-Jul-08 9:22 
GeneralLogin Form with several User in DB?How to do in asp.net Using C# without using Authenticate function.. Pin
Gunaseelan8417-Jun-08 4:08
Gunaseelan8417-Jun-08 4:08 
AnswerRe: Login Form with several User in DB?How to do in asp.net Using C# without using Authenticate function.. Pin
Surajit Paul17-Jun-08 19:08
Surajit Paul17-Jun-08 19:08 
GeneralRe: Login Form with several User in DB?How to do in asp.net Using C# without using Authenticate function.. Pin
anandk1209873-Feb-10 6:08
anandk1209873-Feb-10 6:08 
GeneralPlz Urgent Help Pin
Adwivedi1-Jun-08 19:54
Adwivedi1-Jun-08 19:54 
GeneralRe: Plz Urgent Help Pin
Surajit Paul1-Jun-08 23:40
Surajit Paul1-Jun-08 23:40 
GeneralRe: Plz Urgent Help Pin
madhu_mal128-Jun-09 19:25
madhu_mal128-Jun-09 19:25 
QuestionVoice Chat Pin
informaniac18-May-08 18:58
informaniac18-May-08 18:58 
GeneralHello . Need help urgent. I have some bugs Pin
Kh30pS27-Mar-08 21:38
Kh30pS27-Mar-08 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.