码迷,mamicode.com
首页 > 移动开发 > 详细

使用wcf的双工模式做的一个控制台聊天app

时间:2014-06-18 16:04:35      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:class   blog   ext   get   使用   文件   

//wcf 服务

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

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract(CallbackContract = typeof(iMyclass))]
    public interface IService1
    {
         
        [OperationContract]
        string  Send(string id,string pid, string str);
        [OperationContract]
        string Register(string id);
        [OperationContract]
        List<string> ALLhost();
    
    }
    [ServiceContract]
    public interface iMyclass
    {
        [OperationContract(IsOneWay = true)]//回调函数方法必须加IsOneWay=true
        void Reciver(string str);
    }

    
 
}

  

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

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
    public class Service1 : IService1
    {
        public static Dictionary<string, iMyclass> hostdic;
        public static List<string> allhost;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">发送人</param>
        /// <param name="pid">接受人</param>
        /// <param name="str">内容</param>
        /// <returns></returns>
        public string Send(string id,string pid, string str)
        {
            try
            {
               
                foreach (var d in hostdic)
                {
                    if (d.Key == pid)
                    {
                        d.Value.Reciver(id+"发送:"+str);
                    }
                }
             //iMyclass myclass= OperationContext.Current.GetCallbackChannel<iMyclass>();
             //myclass.Reciver("你好");
                return "1";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public List<string> ALLhost()
        {
            return allhost;
        }
        public string Register(string id)
        {
            if (hostdic == null)
            {
                hostdic = new Dictionary<string, iMyclass>();
            }
            if (allhost == null)
            {
                allhost = new List<string>();
            }
            
                iMyclass imyclass = OperationContext.Current.GetCallbackChannel<iMyclass>();
                hostdic.Add(id, imyclass);

                allhost.Add(id);

            
            return id;
        }
    
    }
}

  //宿主

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

namespace ServerHost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost serverhost = new ServiceHost(typeof(WcfService1.Service1));
            serverhost.Open();
            Console.WriteLine("open");
            Console.ReadKey();
        }
    }
}

  //宿主配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<system.serviceModel>
		<services>
			<service name="WcfService1.Service1">
				<endpoint address="net.tcp://192.168.1.12:3721/calculatorservice"
//改为本地的ip
binding="netTcpBinding" contract="WcfService1.IService1" bindingConfiguration ="TicketBindingConfiguration"/> </service> </services> <bindings> <netTcpBinding> <binding name="TicketBindingConfiguration" openTimeout="00:10:10" receiveTimeout="00:10:10" sendTimeout="00:10:10" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> <readerQuotas maxStringContentLength="6553600" maxArrayLength="6553600" /> </binding> </netTcpBinding> </bindings> </system.serviceModel> </configuration>

  //客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfService1;
using System.ServiceModel;
using System.Configuration;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                myclass myclass = new myclass();
                //  myclass.contentevent += receive;
                InstanceContext callback = new InstanceContext(myclass);
                //  ChannelFactory<IService1> channl = new ChannelFactory<IService1>(  "wcfserver");  如果不是双工模式没有回调的话就使用这个
                DuplexChannelFactory<IService1> channl = new DuplexChannelFactory<IService1>(callback, "wcfserver");
                IService1 IService1 = channl.CreateChannel();
                Console.WriteLine("请输入自己的用户名");
                string id = Console.ReadLine();
                string str = IService1.Register(id.Trim());
                Console.WriteLine(str);
                while (true)
                {
                    Console.WriteLine("输入发送数据");
                    string info = Console.ReadLine();
                    Console.WriteLine("输入接受人");
                    string piduser = Console.ReadLine();
                    Console.WriteLine("发送给" + piduser.Trim() + ":" + info.Trim());
                    IService1.Send(id,piduser.Trim(), info.Trim());
                }
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
    public class myclass : iMyclass
    {
        // public delegate void conten(string str);
        //  public event conten contentevent;
        public void Reciver(string str)
        {
            Console.WriteLine("{0}:" + str, System.DateTime.Now);
            // contentevent(str);
        }

    }
}

  //客户端配置文件

<?xml version="1.0" encoding="utf-8" ?>


<configuration>
	<system.serviceModel>
		<client>

			<endpoint name="wcfserver" address="net.tcp://192.168.1.12:3721/calculatorservice" //改为本地的ip
					  binding="netTcpBinding"
					  contract="WcfService1.IService1"
						bindingConfiguration ="TicketBindingConfiguration"/>

		</client>
		<bindings>
			<netTcpBinding>
				<binding  name="TicketBindingConfiguration"  openTimeout="00:10:10" receiveTimeout="00:10:10"    sendTimeout="00:10:10"  maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
					<security mode="None" />
					<readerQuotas  maxStringContentLength="6553600" maxArrayLength="6553600" />
				</binding>
			</netTcpBinding>
		</bindings>
	</system.serviceModel>
</configuration>

  

使用wcf的双工模式做的一个控制台聊天app,布布扣,bubuko.com

使用wcf的双工模式做的一个控制台聊天app

标签:class   blog   ext   get   使用   文件   

原文地址:http://www.cnblogs.com/wlzhang/p/3791142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!