Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone,

I am newbie to WCF, in fact i have no idea of wcf cooncepts. Now i am in a condotion where i need to complete a chat application using WCF. I have done a bit please help me how to proceed.

Here is my code
I have included 3 projects under single solution. One is library for interfaces

C#
namespace WCF_interface
{
    [ServiceContract(CallbackContract=typeof(IClientCallback),SessionMode=SessionMode.Required)]
    public interface IServiceInterface
    {
        [OperationContract]
        void JoinChat(string username);
        [OperationContract]
        void SendMessageTo(string username, string message);
        [OperationContract]
        void LeaveChat(string username);
        [OperationContract]
        string[] GetOnlineUsersList();
        
       
        
    }
    public interface IClientCallback
    {
        [OperationContract(IsOneWay=true)]
        void ReceiveMessage(string sender, string message);
        [OperationContract(IsOneWay=true)]
        void UserEnter(string username);
        [OperationContract(IsOneWay=true)]
        void UserLeave(string username);

    }
}


Another is ChatServer

C#
namespace ChatServer
{
    public partial class ChatServer : Form
    {
        ServiceHost host = null;
        public ChatServer()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
        private void button1_Click(object sender, EventArgs e)
        {
            host = new ServiceHost(typeof(ChatServerClass));
            host.AddServiceEndpoint(typeof(IServiceInterface), new NetTcpBinding("TcpBinding"), "net.tcp://localhost:2020/ChatService");
            host.Open();
            MessageBox.Show("Server Started");
            
            
        }
        private void button2_Click(object sender, EventArgs e)
        {
            host.Close();
            MessageBox.Show("server stopped");
        }

    }
}


and within ChatServe i have one more class which implements IServiceInterface

C#
namespace ChatServer
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class ChatServerClass : IServiceInterface
    {
        public delegate void UserJoin(string username);
        public delegate void SendMessage(string username, string message);
        public delegate void UserLeft(string username);
        public delegate void GetOnlineUsers(string username);

        public event UserJoin Newjoin;
        public event SendMessage MessageTo;
        public event UserLeft RemoveUser;
        public event GetOnlineUsers AvailableUsers;

              
        bool flag = true;
        List<string> ListofUsers = new List<string>();
        IClientCallback icallback;
        
                           
        [OperationBehavior]
        public void JoinChat(string name)
        {
            checkifuserexists(name);
            if (flag == true)
            {
                ListofUsers.Add(name);
               // Client cs = new Client();
               //Newjoin += new UserJoin(cs.Client_Newjoin);
               MessageBox.Show("User Logged in");
               //Newjoin(name);
            }
            else
                MessageBox.Show("Select other Name");
            
            ListofUsers.ToArray();
            
        }

        [OperationBehavior]
        public void SendMessageTo(string username, string message)
        {
            
            //Newjoin("hello");
        }
        public void ChatServerClass_Newjoin(string name)
        {
            //MessageBox.Show(name);
                                                                                    //Newjoin -= new UserJoin(ChatServerClass_Newjoin);
        }
        
        [OperationBehavior]
        public void LeaveChat(string username)
        {
            ListofUsers.Remove(username);
        }

        [OperationBehavior]
        public string[] GetOnlineUsersList()
        {
            return ListofUsers.ToArray();
        }

        

        public bool checkifuserexists(string name)
        {
            
            for (int i = 0; i < ListofUsers.Count; i++)
            {
                if (name == ListofUsers[i])
                {
                    flag = false;
                    break;
                }
                else
                    flag = true;

            }

            return flag;
        }

        
    }
}</string></string>


And My client side is

C#
namespace WCF_ChatClient
{
    public partial class Client : Form,IClientCallback
    {
        
      
        IServiceInterface client;
        IClientCallback Icall;
        private string sel;
        List<string> ListofUsers = new List<string>();
        //public Dictionary<string,> ListofUsers = new Dictionary<string,>();
        DuplexChannelFactory<iserviceinterface> connect;
       private string username;

       
        public Client()
        {
            InitializeComponent();
            connect = new DuplexChannelFactory<iserviceinterface>(this, "TcpBinding");
            client = connect.CreateChannel();
            
            
        }

        
        public Client(string username)
        {
            this.username = username;
        }
        private void login_btn_Click(object sender, EventArgs e)
        {
            //Client newclient = new Client(login_txt.Text);
            username = login_txt.Text;
            //InstanceContext context = new InstanceContext(Icall);
            //connect.Open();
            client.JoinChat(username);
        }

        public void Client_Newjoin(string name)
        {
            this.login_txt.Focus();
            this.login_txt.ReadOnly = true;
            MessageBox.Show(name);
         }

        
        private void Send_btn_Click(object sender, EventArgs e)
        {
            
            client.SendMessageTo(username, sel);
            rtb_msg.Clear();
        }

        public void ReceiveMessage(string username, string message)
        {
            
        }
     

        public void UserEnter(string username)
        {
            
        }

        public void UserLeave(string username)
        {
        }

         private void button1_Click_1(object sender, EventArgs e)
        {
            client.LeaveChat(username);
             
        }

         private void UsersOnline_cmb_Click(object sender, EventArgs e)
         {
             UsersOnline_cmb.Items.Clear();
             string[] users = client.GetOnlineUsersList();
             foreach (string str in users)
                 UsersOnline_cmb.Items.Add(str);
         }

         private void UsersOnline_cmb_SelectedIndexChanged(object sender, EventArgs e)
         {
             sel = UsersOnline_cmb.SelectedItem.ToString();
         }

    }

  }</iserviceinterface></iserviceinterface></string></string>



The problem is i am not getting how to implement the callbacks and where exactly to implement the callbacks in my code. My client design page consists of textbox, login button, rich text box , and send button and combo box to show list of online users. PLease help me . I have gone through the COdeproject example of chat application but its usin xaml. THe client and server connection is established successfully. Please Help

Thank you
Posted
Updated 19-Oct-11 23:59pm
v2

Hi,
You can read this article:
WCF / WPF Chat Application[^] Sacha created here a chat using WCF and WPF, so you can find answer for your question.
 
Share this answer
 
Comments
Simon Bang Terkildsen 20-Oct-11 10:02am    
Good suggestion
I have gone through that link its using xaml i need it using windows forms. Very simple one is enough. Can you please provide some links or sourcode
 
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