Introduction
In the previous article, we saw how to host Websocket service as windows service.
This time, we will learn the use of the low-level HttpListener class (System.Net.HttpListener), to start a self-hosted service. And the client we will be using is ClientWebSocket (System.Net.WebSockets).
Environment : VS Professional 2012 RC, Windows 8 Server
I will break down this article into the following 2 simple steps:
- Server - HttpListener
- Client - ClientWebSocket
A. Server - HttpListener
- Create a new Project; Windows -> Console Application
Solution Name : WebsocketHttpListener
Project Name: Server
- Add a new class (WebsocketServer.cs) to the Server project
- Start the HttpListener, and continually process the requests received
- Process received request
- Invoke Start, from the Main program thread
B. Client- ClientWebSocket
- Add new Project (Windows -> Console Application) Client to the solution.
- Connect to the listener at the same endpoint to which its listening; the only difference is the protocol over which you are connecting (i.e. WS).
- Once connected, write Send and Receive async Tasks.
Build the solution.
Update the solution properties, so that it runs both applications (Server first and then Client).
Run to test. (Make sure that you are running Visual Studio under Administrative rights.)