This project is read-only.

Sample 01 - ConsoleApp

This is a very simple sample. The project is a console application, and once the connection between the server and the client is established, anything that is typed in the console, will be sent to the client.

The sample contains two files: client.html and Server.cs, they represent the client and the server respectively.


The file contains the javascript needed to create and connect the web socket, and receive data from the server:
// create a new websocket and connect
var ws = new WebSocket('ws://localhost:8181/consoleappsample');

// when data is comming from the server, this metod is called
ws.onmessage = function (evt) {
	inc.innerHTML += + '<br/>';

// when the connection is established, this method is called
ws.onopen = function () {
	inc.innerHTML += '.. connection open<br/>';

// when the connection is closed, this method is called
ws.onclose = function () {
	inc.innerHTML += '.. connection closed<br/>';


This file contains the class the handle the web socket connection server-side, along with the main method, to start the server.

The sockets are handled server-side, much like they are client side, the following class is the "handler" class for the web socket connection:
// The server-side socket
class ConsoleAppSocket : WebSocket
	// This method is called when data is comming from the client.
	// In this example the method is just empty
	public override void Incomming(string data)


	// This method is called when the socket disconnects
	public override void Disconnected()
		Console.WriteLine("--- disconnected ---");

	// This method is called when the socket connects
	public override void Connected(ClientHandshake handshake)
		Console.WriteLine("--- connected --- ");
In the main method the server is created and the above class is registered in the server:
var nugget = new WebSocketServer(8181, "null", "ws://localhost:8181");

// register the ConsoleAppSocket class to handle connection comming to /consoleappsample
Now any client that connects to "ws://localhost:8181/consoleappsample" will be handled by the ConsoleAppSocket class

Last edited Sep 14, 2010 at 5:52 PM by jonlt, version 8


No comments yet.