Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a chat application in which on the login page i have made textbox where user will enter communication channel no.. and that channel no i want to store in array ? Now when the user login to particular channel then it shuld show only users of that communication channel in chat pane.
Here i have used hash table for storing users and For this i have made a class chatengine.cs where i have a function to get list of users using javascript with AJAX
My problem is to select list of login users of particular channel
Plz provide me valuable suggestions with some code hint what should i do.. here is some code of getting chatengine class.
XML
public class ChatEngine : IChatEngine
{
    const string msg = "<li class=\"{0}\">{1}</li>\r\n";
    const string userlistfmt = "<li>{0}</li>\r\n";
    const string serverstyle = "servermsg";
    const string userstyle = "usermsg";
    const string actionstyle = "actionmsg";
    const string timeoutfmt = "User {0} timed out";
    const string textlimitfmt = "{0}, your message was {1} characters long.  The limit is {2} characters.";
    const string nickinusefmt = "{0} a user with this nick already exists";
    const string joinedfmt = "User {0} has joined";
    const string nickfmt = "{0} is now known as {1}";
    const string killfmt = "<strong>User {0} has been terminated!</strong>";
    const string clearfmt = "<strong>User {0} has cleared all chat log!</strong>";
    const string unknowcmdfmt = "<strong>User {0} : Unknow command</strong>";
    const string mefmt = "{0} {1}";
    const string txtfmt = "{0}: {1}";
    const long timerdelay = 30000000;
    const int maxbuffer = 2000;
    const int textlimit = 2000;
    Timer timer;
    Hashtable users;
    StringCollection chat;
    StringCollection pings;
    object syncRoot = new object();
   
    /// Default constructor
   
    public ChatEngine()
    {
        users   = new Hashtable();
        chat    = new StringCollection();
        pings   = new StringCollection();
        TimerCallback OnTimerTick = new TimerCallback( TimerTick );
        timer = new Timer( OnTimerTick, null, timerdelay, timerdelay );
    }
    
    // Check for pinged out users
    
    private void TimerTick( object state )
    {
        lock( syncRoot )
        {
            string[] current = new string[ users.Keys.Count ];
            users.Keys.CopyTo( current, 0 );
            foreach( string guid in current )
            {
                if( !pings.Contains( guid ) )
                {
                    chat.Add(
                        this.MakeServerMessage(
                            string.Format( timeoutfmt, users[ guid ].ToString() )
                        )
                    );
                    users.Remove( guid );
                }
            }
            pings.Clear();
        }
    }
    // Gets the current list of
    // users
    public string[] Users
    {
        get
        {
            string[] nicks = new string[ users.Count ];
            users.Values.CopyTo( nicks, 0 );
            return nicks;
        }
    }
    
    /// HTML Formatted user list
   
public string UserList
    {
        get
        {
            StringBuilder sb = new StringBuilder();
            string[] nicks = new string[ users.Count ];
            users.Values.CopyTo( nicks, 0 );
            foreach( string user in nicks )
            {
                sb.Append( string.Format( userlistfmt, user ) );
            }
            return sb.ToString();
        }
    }
    
   // Gets the current buffer of chat text
   
    public string BufferText
    {
        get
        {
            StringBuilder sb = new StringBuilder();
            foreach( string line in chat )
            {
                sb.Append( line );
            }
            return sb.ToString();
        }
    }
    
    // Does a user exist based on a guid
    
    public bool GuidExists( string guid )
    {
        return users.ContainsKey( guid );
    }
    // Checks to see if the current user
    // exists in the list
    public bool UserExists(string user)
    {
        return users.ContainsValue( user );
    }
    // Adds a user to the list
    
    public void AddUser(string id, string user)
    {
        lock( syncRoot )
            if( !UserExists( user ) )
            {
                users.Add( id, user );
                pings.Add( id );
                chat.Add(
                    this.MakeServerMessage(
                        string.Format( joinedfmt, user )
                    )
                );
            }
    }
    
   
   
    
    }
    
  
}
Posted
Updated 29-Mar-10 17:38pm
v3

Your whole design is rubbish. But, doing a chat in ASP.NET is a dumb idea to start with. However, your rooms should each have a list of people signed in to them.
 
Share this answer
 
How different is this question from your last one?
http://www.codeproject.com/answers/69092/How-can-I-add-chat-rooms-channels-to-the-demo-app-.aspx[^]

Here you have just copied the codebase of the article[^]that you mentioned last time. I still don't see that you have tried anything. Have you?

Where is the code to handle different channel? This is something only left/new to the article that you are referring to. If it's your project, atleast try.. you already are re-using almost the full code.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900