大家
我创建了一个简单的聊天应用程序使用WCF。问题是在回调,我无法上显示的消息到客户端丰富的文本框。即耳语按钮的服务器上的用户点击时回调客户和其他客户端发送的消息必须在RTB上显示,但是这不会发生在我的情况下,如何更新。在客户端丰富的文本框,发送的消息不会被更新。请帮
我使用两个客户端和服务器的WinForms和一个共同的接口
接口的代码是: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);
}
}
服务器代码是
{C}
这上面的服务器代码是在一个单独的类文件和我的主机服务器,使用其他的WinForm这是在相同的服务器托管和客户端连接到我跳过这些部分的server.So project.No与问题。
客户端的是哪些代码是
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
}
}
如何更新消息sent.Please帮助的客户端的富文本框
谢谢