Wcf 新功能--WebSocket、UDP

1.WebSocket通信

websocket 和 http的关系(交集)

要点

    1. 长链接
    2. 使用NetHttpBinding
    3. 基于双工

协议栈

Http + Binary 模式传输 

 Host

 public class HomeService : IHomeService
    {
        public static Dictionary<string, ICallback> dic = new Dictionary<string, ICallback>();

        public string Do(string ms)
        {
            var callback = OperationContext.Current.GetCallbackChannel<ICallback>();
            dic[ms] = callback;
            Console.WriteLine("当前{0}已登录", ms);
            
            return string.Format("datatime:{0},data:{1}", ms, DateTime.Now);
        }

        public static void DoModify()
        {
            while (true)
            {
                var input = Console.ReadLine();

                if (!string.IsNullOrEmpty(input))
                {
                    foreach (var item in dic)
                    {
                        item.Value.Notify(input);
                    }
                }
            }
        }
    }

[ServiceContract(CallbackContract = typeof(ICallback))]
    public interface IHomeService
    {
        [OperationContract]
        string Do(string ms);
    }

    [ServiceContract]
    public interface ICallback
    {
        [OperationContract(IsOneWay =true)]
        void Notify(string msg);
    }
View Code
 static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(HomeService)))
            {
                host.Opened += Host_Opened;
                host.Open();
                Task.Run(() =>
                {
                    HomeService.DoModify();
                });
                Console.ReadKey();
                System.Threading.Thread.Sleep(int.MaxValue);
            }
        }

        private static void Host_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("opened");
        }
View Code
 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    <services>
      <service name="Host06.HomeService">
        <endpoint address="homeservice"
                  binding="netHttpBinding" bindingConfiguration="myNetHttpBinding"
          contract="Host06.IHomeService">

        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.48.128:8080"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings>
      <netHttpBinding>
        <binding name="myNetHttpBinding">
        <!--指定何时使用 Web Socket 
        WhenDuplex    如果为双工协定,则使用 Web Socket 协议。
        Always    始终使用 Web Socket 协议,而不管协定类型。
        Never    永远不使用 Web Socket 协议。-->
          <webSocketSettings transportUsage="WhenDuplex"/>
          <security mode="None"/>

        </binding>
      </netHttpBinding>
    </bindings>
  </system.serviceModel>
View Code

client

class Program
    {
        static void Main(string[] args)
        {
            var instance = new InstanceContext(new MyCallback());
            ServiceReference1.HomeServiceClient client = new ServiceReference1.HomeServiceClient(instance);
            var result = client.Do("hello");
            Console.WriteLine(result);
            Console.ReadKey();
        }


    }

    public class MyCallback : ServiceReference1.IHomeServiceCallback
    {
        public void Notify(string msg)
        {
            Console.WriteLine(msg);
        }
    }
View Code

fiddler抓包结果

2.UDP通信

 <system.serviceModel>
    <client />
    <behaviors>
      <serviceBehaviors>
        <behavior name="myServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service name="Host07.HomeService" behaviorConfiguration="myServiceBehavior">
        <endpoint address="soap.udp://192.168.48.128:8081/"
                  binding="udpBinding"
                  contract="Host07.IHomeService">

        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.48.128:8080"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings />
  </system.serviceModel>
View Code

要点

    1. EndPoint Address :soap.udp://192.168.48.128:8081/
    2. Binding:udpBinding

协议栈

UDP + binary 模式传输

posted @ 2020-02-14 00:13  vvf  阅读(502)  评论(0编辑  收藏  举报