首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WCF双工,实现简略的聊天程序,开他娘的源!(依稀记得LinqToAccess开他娘的源)

2013-12-20 
WCF双工,实现简单的聊天程序,开他娘的源!!(依稀记得LinqToAccess开他娘的源)wcf DLL的接口using Systemus

WCF双工,实现简单的聊天程序,开他娘的源!!(依稀记得LinqToAccess开他娘的源)
wcf DLL的接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CoreDll
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IQQSback))]
    public interface IQQS
    {
        [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
        void Speak(string value);

        [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
        void Whisper(string name, string value);

        [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
        void Leave();

        [OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)]
        List<string> join(string value);

    }

    interface IQQSback
    {
        [OperationContract(IsOneWay = true)]
        void Receive(string senderName, string message);

        [OperationContract(IsOneWay = true)]
        void ReceiveWhisper(string senderName, string message);

        [OperationContract(IsOneWay = true)]
        void UserEnter(List<string> name);

        [OperationContract(IsOneWay = true)]
        void UserLeave(string name);
    }
}


wcf的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CoreDll
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Service1 : IQQS
    {
        IQQSback QQback = OperationContext.Current.GetCallbackChannel<IQQSback>();
        delegate void userenter(List<string> username);
        static event userenter UserEnterEvent;
        delegate void userspeak(string name, string content);
        static event userspeak UserSpeakEvent;
        delegate void userWhisper(string name, string content);
        static SortedList<string, userWhisper> Userspeaklist = new SortedList<string, userWhisper>();
        string Name;

        public List<string> join(string value)
        {
            if (Userspeaklist.Keys.Contains(value))
            {
                return new string[] { "已经有此用户" }.ToList();
            }
            this.Name = value;
            UserSpeakEvent += new userspeak(UserSpeakEventFunction);
            if (UserEnterEvent != null)
            {
                UserEnterEvent(new string[] { value }.ToList());
            }
            UserEnterEvent += new userenter(UserEnterEventFunction);


            Userspeaklist.Add(value, UserWhisperFunction);
            return Userspeaklist.Keys.ToList();

        }

        public void Speak(string value)
        {
            if (UserSpeakEvent != null)
            {
                UserSpeakEvent(this.Name, value);
            }
        }

        public void Whisper(string name, string value)
        {
            if (!Userspeaklist.Keys.Contains(name))
            {
                return;
            }
            Userspeaklist[name](name,value);
        }

        public void Leave()
        {
            UserEnterEvent -= UserEnterEventFunction;
            UserSpeakEvent -= UserSpeakEventFunction;
            Userspeaklist.Remove(this.Name);
            //退出的地方没实现,但是方法跟添加,说话一样。
            QQback.UserLeave(this.Name);
        }



        void UserEnterEventFunction(List<string> username)
        {
            this.QQback.UserEnter(username);
        }

        void UserSpeakEventFunction(string name, string content)
        {
            QQback.Receive(name, content);
        }

        void UserWhisperFunction(string name, string content)
        {
            QQback.ReceiveWhisper(name, content);
        }
    }
}



宿主

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace WCFsuzhu
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("正在启动服务");
            ServiceHost sh = new ServiceHost(typeof(CoreDll.Service1));
            sh.Open();
            Console.WriteLine("启动服务成功!回车键退出。");
            Console.ReadLine();
            sh.Abort();
            sh.Close(); 
        }
    }
}



[解决办法]
  绝对好贴。
帮你顶,
不会沉的。
[解决办法]
看看:
 address 在这里找到的,好象不要分的。大公无私的楼主!

热点排行