Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#

Using WebSocket in .NET 4.5 (Part 1)

Rate me:
Please Sign up or sign in to vote.
4.81/5 (24 votes)
15 Jul 2013CPOL3 min read 237.3K   73   9
In this serial of articles, I will share some knowledge I learnt recently in using WebSocket in .NET 4.5.

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 be IIS 8 and above. IIS 8 Express which is also packaged in Visual Studio 2012 does NOT support WebSocket at present. I hope Microsoft will add support in future. <o:p>

HTML 5 WebSocket API has no limitation on OS platform. The only limitation is the browser versions. Internet Explorer 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. On Windows Server 2012, you could do that through Server Manager -> Manage -> Add Roles and Features. Expand the Web Server (IIS) role and check Web 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.

Click to enlarge image

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 use HttpContext.AcceptWebSocketRequest in a traditional ASP.NET or MVC 4 web application.

Related Links

Using WebSocket in .NET 4.5:  

This article was originally posted at http://blog.csdn.net/pkudzy/article/details/9248217

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
China China
Over 10-years experience in using Microsoft technologies.
At present, working as the architect of a clustered real-time data delivery and visualization system, responsible for the design of component architecture, product packaging and deployment, also targeting private cloud solutions for future.

Comments and Discussions

 
QuestionHosting Pin
SeyedMasoud14-Jan-20 7:47
SeyedMasoud14-Jan-20 7:47 
GeneralGood Explanation Pin
Alireza_13625-Jun-15 23:27
Alireza_13625-Jun-15 23:27 
QuestionHelp: Network Error 12029 Pin
ScotParchman9-Feb-15 12:57
ScotParchman9-Feb-15 12:57 
Questionwin 7 implement? Pin
anssary201017-Sep-14 1:53
anssary201017-Sep-14 1:53 
QuestionHow to use it? Pin
jiang shanshan10-Feb-14 14:23
jiang shanshan10-Feb-14 14:23 
SuggestionWebsockets in Windows 8 Pin
vbguyny12-Dec-13 8:28
vbguyny12-Dec-13 8:28 
QuestionGreat help Pin
Simon_Di31-Oct-13 10:27
Simon_Di31-Oct-13 10:27 
QuestionWhy four articles? Pin
Pete O'Hanlon16-Jul-13 0:18
subeditorPete O'Hanlon16-Jul-13 0:18 
QuestionRidiculous!!! Pin
Dewey15-Jul-13 21:04
Dewey15-Jul-13 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.