Hello everyone
I am creating a simple chat application using wcf. The problem is on callback i am not able to display the message on to client rich text box. i.e when the user clicks on whisper button the server makes a callback to client the callback is successfull, but the message sent wont be updated in the client rich text box. Please help
I am using two winforms for client and server and one common interface
The interface code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace InterfaceLibrary
{
[ServiceContract(CallbackContract = typeof(IChatCallback), SessionMode = SessionMode.Required)]
public interface IChat
{
[OperationContract]
void JoinChat(string username);
[OperationContract]
void SendMessage(string sender, string message);
[OperationContract]
void LeaveChat(string username);
//[OperationContract]
//string[] GetOnlineUsers();
}
public interface IChatCallback
{
[OperationContract(IsOneWay=true)]
void ReceiveMessage(string from, string message);
[OperationContract(IsOneWay = true)]
void UserEnter(string name);
[OperationContract(IsOneWay = true)]
void UserLeave(string name);
}
}
The server code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using InterfaceLibrary;
using System.Windows.Forms;
using ChatClient;
namespace WCFChat
{
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Reentrant)]
class ChatServer:IChat
{
//public delegate void ReceiveMsg(string sender, string message);
//public event ReceiveMsg Receivemessage;
IChatCallback callback = null;
Client cs = new Client();
public static Dictionary<string,> ListofUsers = new Dictionary<string,>();
public void JoinChat(string username)
{
callback = OperationContext.Current.GetCallbackChannel<ichatcallback>();
if (CheckUserExists(username) == true)
{
ListofUsers.Add(username, callback);
}
else
{
MessageBox.Show("Username already exixts!: Please select different Name");
}
ListofUsers.ToArray();
}
public void SendMessage(string sender, string message)
{
foreach (string str in ListofUsers.Keys)
{
//i have hardcoded the username to abc to just check. So when user
//logs in in has abc this portion will be executed and makes call
//to ReceiveMessage of client where the textbox needs to be updated
//with the message sent. But in my case textbox is not updated.
if (str == "abc")
{
IChatCallback icall = ListofUsers[str];
icall.ReceiveMessage(sender,message);
//Receivemessage += new ReceiveMsg(icall.ReceiveMessage);
//Receivemessage(sender, message);
}
}
}
public void LeaveChat(string name)
{
}
//public string[] GetOnlineUsers()
//{
// return ;
//}
public bool CheckUserExists(string username)
{
foreach (string name in ListofUsers.Keys)
{
if(username.Equals(name,StringComparison.OrdinalIgnoreCase))
return false;
}
return true;
}
}
}
This above server code is in a separate class file and i host the server using another winform which is under the same project.No problem with server hosting and clients connecting to the server.So i am skipping those parts.
The client which code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using InterfaceLibrary;
using System.Diagnostics;
using System.Threading;
namespace ChatClient
{
[CallbackBehavior(UseSynchronizationContext = false)]
public partial class Client : Form,IChatCallback
{
public delegate void ReceiveMsg(string sender, string message);
public event ReceiveMsg Receivemessage;
IChat chatter = null;
DuplexChannelFactory<ichat> connect;
InstanceContext instanceContext = null;
public string CurrentUser;
public Client()
{
InitializeComponent();
}
private void login_btn_Click(object sender, EventArgs e)
{
//CallBackHandler instance = new CallBackHandler();
//instanceContext = new InstanceContext(new CallBackHandler());
Client instance = new Client();
instanceContext = new InstanceContext(new Client());
connect = new DuplexChannelFactory<ichat>(instanceContext, "TcpBinding");
chatter = connect.CreateChannel();
if (!string.IsNullOrEmpty(login_txt.Text))
{
CurrentUser = login_txt.Text;
chatter.JoinChat(CurrentUser);
}
else
{
MessageBox.Show("Please Enter Valid UserName", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//When user clicks on whisper button it goes to server sendmessage method
//Where callback is implemented
private void whispher_btn_Click(object sender, EventArgs e)
{
chatter.SendMessage(login_txt.Text, rtxt_message.Text);
}
#region IChatCallback Members
//the callback calls this method, but the rich textbox wont be updated
public void ReceiveMessage(string from, string message)
{
rtxt_message.AppendText(from+":"+message);
}
public void UserEnter(string name)
{
throw new NotImplementedException();
}
public void UserLeave(string name)
{
throw new NotImplementedException();
}
#endregion
}
}
How to update the richtext box of the client with the message sent.Please help
Thank you