如何使用HTML5的WebSocket实现网页与服务器的双工通信(二)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 如何使用HTML5的WebSocket实现网页与服务器的双工通信(二)

如何使用HTML5的WebSocket实现网页与服务器的双工通信(二)

 2017/12/8 11:14:49  图图的耳朵  程序员俱乐部  我要评论(0)
  • 摘要:本系列服务端双工通信包括两种实现方式:一、使用Socket构建;二、使用WCF构建。本文为使用WCF构建服务端的双工通信,客户端同样使用Html5的WebSocket技术进行调用。一、创建WCF服务库Wcf.Duplex.Library:1.定义协议的服务接口:[ServiceContract]publicinterfaceIWebSocketEchoCallback{[OperationContract(IsOneWay=true,Action="*")]voidSend
  • 标签:Web 实现 使用 服务器 HTML5 服务 网页 socket

本系列服务端双工通信包括两种实现方式:一、使用Socket构建;二、使用WCF构建。本文为使用WCF构建服务端的双工通信,客户端同样使用Html5的WebSocket技术进行调用。

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

1.定义协议的服务接口

[ServiceContract]
public interface IWebSocketEchoCallback
{
[OperationContract(IsOneWay = true, Action = "*")]
void Send(Message message);
}

[ServiceContract(CallbackContract = typeof(IWebSocketEchoCallback))]
public interface IWebSocketEcho
{
[OperationContract(IsOneWay = true, Action = "*")]
void Receive(Message message);
}

注意:

OperationContract中的Action,一定要设置。

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

IWebSocketEchoCallback 回调接口中:可以定义多个OperationContract(IsOneWay = true, Action = "*")方法

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

public class EchoService : IWebSocketEcho
{

IWebSocketEchoCallback _callback = null;

public EchoService()
{

//获取回调信道
_callback =
OperationContext.Current.GetCallbackChannel<IWebSocketEchoCallback>();

Timer time = new Timer(10000);
time.Elapsed += time_Elapsed;
time.Start();
}
void time_Elapsed(object sender, ElapsedEventArgs e)
{
_callback.Send(CreateMessage("Message From WebSockets Host" + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
}
public void Receive(Message message)
{
if (message == null)
{
throw new ArgumentNullException("message");
}

WebSocketMessageProperty property =
(WebSocketMessageProperty)message.Properties["WebSocketMessageProperty"];
WebSocketContext context = property.WebSocketContext;
var queryParameters = HttpUtility.ParseQueryString(context.RequestUri.Query);
string content = string.Empty;

if (!message.IsEmpty)
{
byte[] body = message.GetBody<byte[]>();
content = Encoding.UTF8.GetString(body);
}

// Do something with the content/queryParams
// ...

string str = null;
if (string.IsNullOrEmpty(content)) // Connection open message
{
str = "Opening connection from user " +
queryParameters["Name"].ToString();
}
else // Message received from client
{
str = "Received message: " + content;
}
wcf.websocket.forweb.LogHelper.log.Error(str);
_callback.Send(CreateMessage(str));
}

private Message CreateMessage(string content)
{
Message message = ByteStreamMessage.CreateMessage(
new ArraySegment<byte>(
Encoding.UTF8.GetBytes(content)));
message.Properties["WebSocketMessageProperty"] =
new WebSocketMessageProperty { MessageType = WebSocketMessageType.Text };

return message;
}
}

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

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

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

说明:增加服务路由的时候,路由前缀可以随便设置;ServiceType,需要设置为WCF服务库的EchoService类。

2.配置WEB.Config:

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

3.在当前项目中增加页面testWebSocket.html,使用WebSocket调用服务

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

 


 同系列其他文章:如何使用HTML5的WebSocket实现网页与服务器的双工通信(一)

发表评论
用户名: 匿名