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
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
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
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);
MessageBox.Show("User Logged in");
}
else
MessageBox.Show("Select other Name");
ListofUsers.ToArray();
}
[OperationBehavior]
public void SendMessageTo(string username, string message)
{
}
public void ChatServerClass_Newjoin(string name)
{
}
[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
namespace WCF_ChatClient
{
public partial class Client : Form,IClientCallback
{
IServiceInterface client;
IClientCallback Icall;
private string sel;
List<string> ListofUsers = new List<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)
{
username = login_txt.Text;
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