本系列服务端双工通讯包涵三种达成格局:壹、使用Socket营造;二、使用WCF塑造。本文为利用WCF构建服务端的双工通讯,客户端同样利用Html伍的WebSocket技术拓展调用。

本种类服务端双工通讯包蕴三种实现格局:1、使用Socket创设;二、使用WCF营造。本文为利用WCF营造服务端的双工通讯,客户端同样选择Html伍的WebSocket技术拓展调用。

前言:         

 

*WCF初探-5:WCF新闻沟通方式之双工通信(Duplex)***博文中,小编执教了双工通信服务的四个利用场景,即订阅和宣布情势,那一篇,作者将经过3个音信发送的例子讲解一下WCF客户端怎么着为双工服务创制回调对象。

双工服务钦赐2个回调协定,客户端应用程序必须贯彻该协定以便提供叁个该服务能够基于协定必要调用的回调对象。就算回调对象不是总体的劳务(例如,您不能利用回调对象运行多个坦途),可是为了落到实处和配备,那几个回调对象足以被视为1种服务。

双工服务的客户端必须:

  • 落成3个回调协定类。
  • 创制回调协定达成类的一个实例,并应用该实例创制传递给 WCF
    客户端构造函数的 System.ServiceModel.InstanceContext 对象。
  • 调用操作并拍卖操作回调。

       双工 WCF
客户端对象除了会公然帮忙回调所必不可缺的效果(包含回调服务的安排)以外,其余的功效和它们的非双工对应项相同。

 

一、创建WCF服务库Wcf.Duplex.Library:

一、创建WCF服务库Wcf.Duplex.Library:

示范表达:

 

  • Service服务契约中定义了八个出殡和埋葬方法Send,采取IsOneWay=true,供客户端调用,向服务端发送消息。Service中还提供了用于双工通信的回调接口IMessageExchangeCallback,该接口中定义了服务端接收音信后向客户端发送新闻的不2诀要Receive,此措施正是客户端发送音信到服务端后,服务端调用回调方法,将音讯发送到客户端。
  • Client须要达成双工协定回调接口的类CallBackHandler,并落到实处Receive方法。
  • 本着双工协定生成的 WCF 客户端须要在构造时提供二个 InstanceContext
    类。此 InstanceContext
    类用作达成回调接口并拍卖从劳动发送回的音信的靶子所在的任务。InstanceContext
    类是用 CallbackHandler
    类的实例构造的。此指标处理通过回调接口从服务发送到客户端的消息。

  图片 1

 

壹.概念协议的劳务接口:

1.概念协议的劳务接口:

 WCF客户端为双工服务创建回调对象示例

 

  • 化解方案如下:

  图片 2

  • 工程协会表达:
  1. Service:类库程序,定义服务契约接口和回调接口,实现劳务契约。在IMessageExchange中定义了Send方法,并且还定义了双工服务回调接口 IMessageExchangeCallback。

    IMessageExchange.cs的代码如下:

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

namespace Service
{
    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMessageExchangeCallback))]
    public interface IMessageExchange
    {
        [OperationContract(IsOneWay=true)]
        void Send(string message);
    }

    public interface IMessageExchangeCallback
    {
        [OperationContract(IsOneWay = true)]
        void Receive(string message);
    }
}                                                

    MessageExchange.cs的代码如下: 

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

namespace Service
{ 
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class MessageExchange : IMessageExchange
    {
        public void Send(string message)
        {
            Console.WriteLine("服务端监听客户端发出的消息:" + message);
            Callback.Receive(message);

        }

        IMessageExchangeCallback Callback
        {
            get
            {
                return OperationContext.Current.GetCallbackChannel<IMessageExchangeCallback>();
            }
        }
    }
}

专注:回调契约接口IMessageExchangeCallback中的Receive方法是在客户端达成的,所以借使要求在服务端调用回调方法就必须通过当前操作的实例上下文来收获,即上面代码中的Callback对象。

  贰.
 Host:控制台应用程序。提供劳动寄宿程序,添加对Srevice程序集的引用。完毕布局文件和代码就足以承接服务。

       Program.cs的代码如下:

  图片 3图片 4

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

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(MessageExchange)))
            {
                host.Opened += delegate { Console.WriteLine("服务已经启动,按任意键终止!"); };
                host.Open();
                Console.Read();
            }
        }
    }
}

View Code

    App.config的代码如下:

  图片 5图片 6

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>

        <services>
            <service name="Service.MessageExchange" behaviorConfiguration="mexBehavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:1234/MessageExchange/"/>
                    </baseAddresses>
                </host>
                <endpoint address="" binding="wsDualHttpBinding" contract="Service.IMessageExchange" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>

        <behaviors>
            <serviceBehaviors>
                <behavior name="mexBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>

    </system.serviceModel>
</configuration>

View Code

  叁.
 Client:控制台应用程序。客户端程序,运行服务承载程序Host后,添加对劳动地点的引用,将命名空间修改为

    MessageExchangeServiceRef,之后在Program.cs达成对双工服务回调接口IMessageExchangeCallback的贯彻和对服务情势的调用。Program.cs的代码如下:

using System;
using Client.MessageExchangeServiceRef;
using System.ServiceModel;

namespace Client1
{

    public class CallBackHandler : IMessageExchangeCallback
    {
        public void Receive(string message)
        {
            Console.WriteLine("客户端监听服务端接收的消息:" + message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            InstanceContext instanceContext = new InstanceContext(new CallBackHandler());
            MessageExchangeClient proxy = new MessageExchangeClient(instanceContext);
            proxy.Send("Wcf Duplex");

            Console.Read();
        }
    }
}

  运维结果如下:

  图片 7

图片 8图片 9

图片 10图片 11

总结:

 

  • 本文模拟了客户端向服务端发送音信,服务端接收到音信后,将监听到的新闻呈现在客户端。希望经过本示例对双工通讯有提高一的认识,关于实例和对话,笔者将在背后的博文中做分析。

图片 12

 1 [ServiceContract]
 2 public interface IWebSocketEchoCallback
 3 {
 4 [OperationContract(IsOneWay = true, Action = "*")]
 5 void Send(Message message);
 6 }
 7 
 8 [ServiceContract(CallbackContract = typeof(IWebSocketEchoCallback))]
 9 public interface IWebSocketEcho
10 {
11 [OperationContract(IsOneWay = true, Action = "*")]
12 void Receive(Message message);
13 }
 1 [ServiceContract]
 2 public interface IWebSocketEchoCallback
 3 {
 4 [OperationContract(IsOneWay = true, Action = "*")]
 5 void Send(Message message);
 6 }
 7 
 8 [ServiceContract(CallbackContract = typeof(IWebSocketEchoCallback))]
 9 public interface IWebSocketEcho
10 {
11 [OperationContract(IsOneWay = true, Action = "*")]
12 void Receive(Message message);
13 }

View Code

View Code

注意:

注意:

OperationContract中的Action,一定要安装。

OperationContract中的Action,一定要安装。

IWebSocketEcho接口中:只好定义1个OperationContract(IsOneWay = true,
Action = “*”)方法入口,否则使用WebSocket连接时会报错

IWebSocketEcho接口中:只可以定义1个OperationContract(IsOneWay = true,
Action = “*”)方法入口,不然使用WebSocket连接时会报错

IWebSocketEchoCallback
回调接口中:能够定义多个OperationContract(IsOneWay = true, Action =
“*”)方法

IWebSocketEchoCallback
回调接口中:能够定义八个OperationContract(IsOneWay = true, Action =
“*”)方法

二.兑现劳务协议,并定义三个时钟,定时调用回调方法,发送新闻给客户端:

2.完毕服务协议,并定义一个石英钟,定时调用回调方法,发送信息给客户端:

图片 13图片 14

图片 15图片 16

 1 public class EchoService : IWebSocketEcho
 2 {
 3 
 4 IWebSocketEchoCallback _callback = null;
 5 
 6 public EchoService()
 7 {
 8 
 9 //获取回调信道
10 _callback =
11 OperationContext.Current.GetCallbackChannel<IWebSocketEchoCallback>();
12 
13 Timer time = new Timer(10000);
14 time.Elapsed += time_Elapsed;
15 time.Start();
16 }
17 void time_Elapsed(object sender, ElapsedEventArgs e)
18 {
19 _callback.Send(CreateMessage("Message From WebSockets Host" + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
20 }
21 public void Receive(Message message)
22 {
23 if (message == null)
24 {
25 throw new ArgumentNullException("message");
26 }
27 
28 WebSocketMessageProperty property =
29 (WebSocketMessageProperty)message.Properties["WebSocketMessageProperty"];
30 WebSocketContext context = property.WebSocketContext;
31 var queryParameters = HttpUtility.ParseQueryString(context.RequestUri.Query);
32 string content = string.Empty;
33 
34 if (!message.IsEmpty)
35 {
36 byte[] body = message.GetBody<byte[]>();
37 content = Encoding.UTF8.GetString(body);
38 }
39 
40 // Do something with the content/queryParams
41 // ...
42 
43 string str = null;
44 if (string.IsNullOrEmpty(content)) // Connection open message
45 {
46 str = "Opening connection from user " +
47 queryParameters["Name"].ToString();
48 }
49 else // Message received from client
50 {
51 str = "Received message: " + content;
52 }
53 wcf.websocket.forweb.LogHelper.log.Error(str);
54 _callback.Send(CreateMessage(str));
55 }
56 
57 private Message CreateMessage(string content)
58 {
59 Message message = ByteStreamMessage.CreateMessage(
60 new ArraySegment<byte>(
61 Encoding.UTF8.GetBytes(content)));
62 message.Properties["WebSocketMessageProperty"] =
63 new WebSocketMessageProperty { MessageType = WebSocketMessageType.Text };
64 
65 return message;
66 }
67 }
 1 public class EchoService : IWebSocketEcho
 2 {
 3 
 4 IWebSocketEchoCallback _callback = null;
 5 
 6 public EchoService()
 7 {
 8 
 9 //获取回调信道
10 _callback =
11 OperationContext.Current.GetCallbackChannel<IWebSocketEchoCallback>();
12 
13 Timer time = new Timer(10000);
14 time.Elapsed += time_Elapsed;
15 time.Start();
16 }
17 void time_Elapsed(object sender, ElapsedEventArgs e)
18 {
19 _callback.Send(CreateMessage("Message From WebSockets Host" + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
20 }
21 public void Receive(Message message)
22 {
23 if (message == null)
24 {
25 throw new ArgumentNullException("message");
26 }
27 
28 WebSocketMessageProperty property =
29 (WebSocketMessageProperty)message.Properties["WebSocketMessageProperty"];
30 WebSocketContext context = property.WebSocketContext;
31 var queryParameters = HttpUtility.ParseQueryString(context.RequestUri.Query);
32 string content = string.Empty;
33 
34 if (!message.IsEmpty)
35 {
36 byte[] body = message.GetBody<byte[]>();
37 content = Encoding.UTF8.GetString(body);
38 }
39 
40 // Do something with the content/queryParams
41 // ...
42 
43 string str = null;
44 if (string.IsNullOrEmpty(content)) // Connection open message
45 {
46 str = "Opening connection from user " +
47 queryParameters["Name"].ToString();
48 }
49 else // Message received from client
50 {
51 str = "Received message: " + content;
52 }
53 wcf.websocket.forweb.LogHelper.log.Error(str);
54 _callback.Send(CreateMessage(str));
55 }
56 
57 private Message CreateMessage(string content)
58 {
59 Message message = ByteStreamMessage.CreateMessage(
60 new ArraySegment<byte>(
61 Encoding.UTF8.GetBytes(content)));
62 message.Properties["WebSocketMessageProperty"] =
63 new WebSocketMessageProperty { MessageType = WebSocketMessageType.Text };
64 
65 return message;
66 }
67 }

View Code

View Code

2.新建WEB项目,在品种中引用第2步建立的WCF服务库Wcf.Duplex.Library。

2.新建WEB项目,在品种中引用第二步建立的WCF服务库Wcf.Duplex.Library。

1.打开Global.asax.cs的文件,在Application_Start扩张以下代码:

1.打开Global.asax.cs的文件,在Application_Start扩大以下代码:

RouteTable.Routes.Add(new ServiceRoute("EchoService",new ServiceHostFactory(),typeof(EchoService)));
RouteTable.Routes.Add(new ServiceRoute("EchoService",new ServiceHostFactory(),typeof(EchoService)));

 

 

表明:扩大服务路由的时候,路由前缀能够不管设置;ServiceType,须求设置为WCF服务库的EchoService类。

证实:扩展服务路由的时候,路由前缀能够任由设置;ServiceType,供给设置为WCF服务库的EchoService类。

2.配置WEB.Config:

2.配置WEB.Config:

图片 17图片 18

图片 19图片 20

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

<services>

<service name="Wcf.Duplex.Library.EchoService"><!--此处为服务类,需要修改为本项目的服务类-->
<endpoint address=""

binding="customBinding"

bindingConfiguration="webSocket"

contract="Wcf.Duplex.Library.IWebSocketEcho" /><!--此处为服务的协议接口,需要修改为本项目对应的服务接口-->
</service>
</services>
<bindings>
<customBinding>
<binding name="webSocket">
<byteStreamMessageEncoding/>
<httpTransport>
<webSocketSettings transportUsage="Always"

createNotificationOnConnection="true"/>
</httpTransport>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

<services>

<service name="Wcf.Duplex.Library.EchoService"><!--此处为服务类,需要修改为本项目的服务类-->
<endpoint address=""

binding="customBinding"

bindingConfiguration="webSocket"

contract="Wcf.Duplex.Library.IWebSocketEcho" /><!--此处为服务的协议接口,需要修改为本项目对应的服务接口-->
</service>
</services>
<bindings>
<customBinding>
<binding name="webSocket">
<byteStreamMessageEncoding/>
<httpTransport>
<webSocketSettings transportUsage="Always"

createNotificationOnConnection="true"/>
</httpTransport>
</binding>
</customBinding>
</bindings>
</system.serviceModel>

View Code

View Code

 

 

三.在现阶段项目中扩展页面testWebSocket.html,使用WebSocket调用服务

三.在当下项目中追加页面testWebSocket.html,使用WebSocket调用服务

图片 21图片 22

图片 23图片 24

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebSockets客户端示例</title>
</head>
<script>
var webSocket;
function connect() {
try {
var readyState = new Array("正在连接", "已建立连接", "正在关闭连接", "已关闭连接");
var host = 'ws://localhost:61413' +
window.location.pathname.replace('testWebSocket.html', 'EchoService') +
'?Name=liza';
webSocket = new WebSocket(host);
var message = document.getElementById("message");
message.innerHTML += "<p>Socket状态:" + readyState[webSocket.readyState] + "</p>";
webSocket.onopen = function () {
message.innerHTML += "<p>Socket状态:" + readyState[webSocket.readyState] + "</p>";
}
webSocket.onmessage = function (msg) {
message.innerHTML += "<p>接收信息:" + msg.data + "</p>";
}
webSocket.onclose = function () {
message.innerHTML += "<p>Socket状态:" + readyState[webSocket.readyState] + "</p>";
}
}
catch (exception) {
message.innerHTML += "<p>有错误发生</p>";
}
}
function send() {
var text = document.getElementById("text").value;
var message = document.getElementById("message");
if (text == "") {
message.innerHTML += "<p>请输入一些文字</p>";
return;
}
try {
webSocket.send(text);
message.innerHTML += "<p>发送数据:" + text + "</p>";
}
catch (exception) {
message.innerHTML += "<p>发送数据出错</p>";
}
document.getElementById("text").value = "";
}
function disconnect() {
webSocket.close();
}
</script>
<body>
<h1>WebSocket客户端示例</h1>
<div id="message"></div>
<p>请输入一些文字</p>
<input id="text" type="text">
<button id="connect" onclick="connect();">建立连接</button>
<button id="send" onclick="send();">发送数据</button>
<button id="disconnect" onclick="disconnect();">断开连接</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebSockets客户端示例</title>
</head>
<script>
var webSocket;
function connect() {
try {
var readyState = new Array("正在连接", "已建立连接", "正在关闭连接", "已关闭连接");
var host = 'ws://localhost:61413' +
window.location.pathname.replace('testWebSocket.html', 'EchoService') +
'?Name=liza';
webSocket = new WebSocket(host);
var message = document.getElementById("message");
message.innerHTML += "<p>Socket状态:" + readyState[webSocket.readyState] + "</p>";
webSocket.onopen = function () {
message.innerHTML += "<p>Socket状态:" + readyState[webSocket.readyState] + "</p>";
}
webSocket.onmessage = function (msg) {
message.innerHTML += "<p>接收信息:" + msg.data + "</p>";
}
webSocket.onclose = function () {
message.innerHTML += "<p>Socket状态:" + readyState[webSocket.readyState] + "</p>";
}
}
catch (exception) {
message.innerHTML += "<p>有错误发生</p>";
}
}
function send() {
var text = document.getElementById("text").value;
var message = document.getElementById("message");
if (text == "") {
message.innerHTML += "<p>请输入一些文字</p>";
return;
}
try {
webSocket.send(text);
message.innerHTML += "<p>发送数据:" + text + "</p>";
}
catch (exception) {
message.innerHTML += "<p>发送数据出错</p>";
}
document.getElementById("text").value = "";
}
function disconnect() {
webSocket.close();
}
</script>
<body>
<h1>WebSocket客户端示例</h1>
<div id="message"></div>
<p>请输入一些文字</p>
<input id="text" type="text">
<button id="connect" onclick="connect();">建立连接</button>
<button id="send" onclick="send();">发送数据</button>
<button id="disconnect" onclick="disconnect();">断开连接</button>
</body>
</html>

View Code

View Code

 

 



 同类别别的文章:如何使用HTML伍的WebSocket达成网页与服务器的双工通讯(1)

 同体系其余文章:哪些使用HTML5的WebSocket达成网页与服务器的双工通信(一)

相关文章