关闭

Using WebSocket in .NET 4.5 (Part 1)

标签: WebSocketWCF.NETIISC#
2061人阅读 评论(3) 收藏 举报
分类:

Introduction

In this serial of articles, I will share some knowledge I learnt recently in using WebSocket in .NET 4.5.

Protocol Overview

The WebSocket protocol was standardized by IETF as RFC 6455. We could find introductions and more information on Wikipedia and W3C.

A WebSocket connection is established by a HTTP handshake (prefixed by “ws://” or “wss://”) between the client and the server. For example (from Wikipedia), the client sends a request:

GET /mychat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat
Sec-WebSocket-Version: 13
Origin: http://example.com

Then the server sends a response to accept the connection:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat

Sec-WebSocket-Key” and “Sec-WebSocket-Accept” are used for confirming the connection between the client and the server through an algorithm of key validation. 

Sec-WebSocket-Version” is used by the client to tell the server which version of the protocol is supported on the client side. No backward compatibility is required in the standard. The server could send a version list through this header if the client specified version is not supported by the server. “13” (1.3) is a milestone version of WebSocket.

Sec-WebSocket-Protocol” is used to confirm the customized sub-protocol of the connection. This enables both selecting a sub-protocol from the client side and being sure that the server agreed to serve that sub-protocol.

For more details about the protocol, please refer to RFC 6455.

WebSocket in .NET

Microsoft .NET 4.5 provides several ways in using WebSocket.

On the server side, we can host our WebSocket server through any one of the ways below:

  • Using HttpContext.AcceptWebSocketRequest.
  • Creating a WCF service with CallbackContract and the new netHttpBinding.
  • Using WebSocketHandler or WebSocketHost provided in Microsoft.WebSockets.dll.

On the client web side, HTML 5 provides WebSocket APIs and jQuery wraps those APIs for easier usage. If we want to create a client side application, we can do that through either of the ways below:

  • Using the ClientWebSocket class (System.Net.WebSockets).
  • Creating a WCF client and referencing the WCF service which supports WebSocket connection.

NOTE: we can only use .NET WebSocket on Windows 8, Windows Server 2012 and above, including both server-side and client-side applications. And the web server must beIIS 8 and above. IIS 8Expresswhich is also packaged in Visual Studio 2012 does NOT support WebSocket at present. I hope Microsoft will add support in future.

HTML 5 WebSocket API has no limitation on OS platform. The only limitation is the browser versions. IE started supporting HTML 5 WebSocket since IE 10.

Preparation     

To enable WebSocket on the server side, you need to install WebSocket Protocol for IIS 8. OnWindows Server 2012, you could do that throughServer Manager =>Manage =>Add Roles and Features. Expand theWeb Server (IIS) role and checkWeb Server => Application Development =>WebSocket Protocol. You may be asked to install dependencies and just allow them. Then install all your selections.Windows 8 should be similar.


You might meet an exception when you test your WebSocket applications.The exception says:

Could not load type ‘System.ServiceModel.Activation.HttpModule’ from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'"

Please refer to this article for solutions.

Summary

Next in Part 2, I will demonstrate how to useHttpContext.AcceptWebSocketRequest in a traditional ASP.NET or MVC 4 web application.

Related Links

Using WebSocket in .NET 4.5:

0
0

猜你在找
【视频】C语言及程序设计(讲师:贺利坚)
【视频】Python爬虫工程师培养课程全套(讲师:韦玮)
【视频】Python全栈开发入门与实战(讲师:李杰)
【视频】2017软考网络规划设计师套餐(讲师:任铄)
【视频】2017软考软件设计师套餐(讲师:任铄)
【视频】2017软考信息系统项目管理师套餐(讲师:任铄)
【视频】软考(高级)项目经理实战营(讲师:张传波)
【视频】微信公众平台开发套餐(讲师:刘运强)
深度学习原理+实战+算法+主流框架套餐(讲师:唐宇迪)
2017系统集成项目管理工程师通关套餐(讲师:徐朋)
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16942次
    • 积分:329
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    最新评论