从WCF中的服务器更新回调中的客户端文本框 [英] Updating client text box on callback from server in wcf

查看:90
本文介绍了从WCF中的服务器更新回调中的客户端文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在使用wcf创建一个简单的聊天应用程序.问题是在回调上,我无法在客户端富文本框中显示消息.即,当用户单击悄悄话"按钮时,服务器会回调客户端,而其他客户端发送的消息必须显示在实时出价工具上,但是在我的情况下,这是没有发生的,如何进行更新.发送的消息将不会在客户端富文本框中更新.请帮忙

我正在为客户端和服务器使用两个winforms和一个公共界面

接口代码为:

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 and the message sent by other client has to be displayed on the RTB, but this is not happening in my case, how to update it. 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;
        }
    }
}



上面的服务器代码位于单独的类文件中,我使用同一项目下的另一个Winform托管服务器.服务器托管和客户端连接到服务器没有问题,因此我跳过了这些部分.

客户端的代码是



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
    }
}



如何使用发送的消息更新客户端的RTF文本框.请帮助

谢谢



How to update the richtext box of the client with the message sent.Please help

Thank you

推荐答案

尝试:
public void ReceiveMessage(string from, string message)
{
 rtxt_message.Dispatcher.Invoke(
  System.Windows.Threading.DispatcherPriority.Normal,
   new Action( delegate()
             {
               rtxt_message.AppendText(from+":"+message);
             }
         ));
}



Dispatcher.Invoke [



Dispatcher.Invoke [^]

Best regards
Espen Harlinn


这篇关于从WCF中的服务器更新回调中的客户端文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆