This project is read-only.

Nugget Documentation

For further reading please take a look at the article at: http://www.codeproject.com/KB/webservices/c_sharp_web_socket_server.aspx
This documentation applies to the POC release: http://nugget.codeplex.com/releases/view/46504

The Server

Server instantiation

The server is instantiated with the following constructor:
var wss = new WebSocketServer(int port, string origin, string location);
where
  • port is the port the server should run on/listen to
  • origin is the origin of the connections i.e. the url of the client script.
  • location is the location of the server e.g. ws://localhost:port/somewhere

Accepting Connections

The server fires an event when a client connects. Add a delegate to that event to handle new connections:

wss.ClientConnected = new ClientConnectedEventHandler(OnClientConnected);
Once a client has connected a new WebSocketConnection object is instantiated. This object fires events upon incoming data, and when the client disconnects:

void OnClientConnected(WebSocketConnection sender, EventArgs e)
{
    sender.Disconnected += new WebSocketDisconnectedEventHandler(OnClientDisconnected);
    sender.DataReceived += new DataReceivedEventHandler(OnClientMessage);
}

Reading incoming data

Incomming data can be handled in the method assigned to the DataReceived event

void OnClientMessage(WebSocketConnection sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.data);
}

Sending data

Sending data to a client is done with the Send method on the WebSocketConnection object associated with the client:
void Send(string str)
where str is the string to send to the client.

The client

The client code is written in JavaScript, and is not specific to this implementation, but a part of HTML5:

Connection to the server:
var ws = new WebSocket("server_location");

Events:
ws.onopen = function () {
    // the connection has been established
};
 
ws.onmessage = function (evt) {  
    // a message (evt.data) has been received from the server
};
 
ws.onclose = function () {
    // the connection has been closed
};

Last edited Jun 7, 2010 at 12:55 AM by jonlt, version 1

Comments

No comments yet.