Click here to Skip to main content
15,895,656 members
Articles / Web Development / ASP.NET

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 385.2K   41.8K   139  
This is a cool chat application created in DotNet using web services having all the functionalities.
using System;
using System.Web;
using System.Data;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[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();        
    }
    [WebMethod]
    public string GetUsers() 
    {
        string strUser = string.Empty;        
        for (int i = 0; i < arrUsers.Count; i++)
        {
            strUser = strUser + arrUsers[i].ToString() + "|";
        }
        return strUser;
    }

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

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

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

    [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;
    }
    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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