Google

Friday, December 10, 2010

WebSockets

It's been a really long time since I made a post on my blog, but lately I had to do some work that resulted in me implementing a draft spec of the WebSocket protocol. I have noticed that there is a lot of talk about them due to the development of HTML5. So I sat down with the latest draft spec for the WebSocket protocol, draft-ietf-thewebsocketprotocol-03, and began implementing a solution that more completely represented the spec and was also usable.

While I wanted to implement the full spec, I did decide to leave out a few parts. I did not implement support for Framing, which at the time of writing is not possible from the WebSocket API for JavaScript. Since my implementation was intended to communicate with browsers, and the API does not have a way for JS code to tell the socket to send data Framed. Because of that, I left it out. I have also not added support for wss, encrypted connections. I will add this at a later time.

So lets get to what I did implement. The WebSocketServer class can listen on a defined IP and port. It will listen for connection attempts and Create WebSocket objects to handle each connection. So unlike a number of sources on the net, this will handle multiple connections at one time. At this time I have not set a limit to the number of them that can be open at one time. The WebSocket class handles the connection from that point on. It will perform the handshake response to the client to establish a good connection. I will then listen for data from the client and raise an event when a complete message has been received. It also provides a way to send data to the client. The code also takes care of dropped connections by cleaning up after a connection failure.

The current code can be found here.

Let me know any suggestions you may have.

11 comments:

Anonymous said...

Hi,can you send me a sample how to use the class. thx.

my email is HL23@live.nl

jkworth said...

The WebSocketServer class is an implementation. It uses the WebSocket class internally. The WebSocket class has a Send function and the WebSocketServer defaults to listen for incoming messages on all WebSocket connections.

Carlo said...

Hi, thank you for this library. However I would know ho use it. I have created a main class that start the WebSocketServer in this way:
static void Main(string[] args)
{
WebSocketServer server = new WebSocketServer();
server.Start();
}

then I have created a Javascript client:

var socket = new WebSocket('ws://localhost:8080/');



socket.onopen = function() {

alert('handshake successfully established. May send data now...');

};

socket.onclose = function() {

alert('connection closed');

};

but I have always the alert that prints 'connection closed'.

Is this the right way to use your library? Or I missing something?

Thanks!

jkworth said...

Carlo,

From the code you posted it seems that your js code is not connecting to the correct port. The empty constructor for the websocketserver defaults to localhost:80 not 8080. The rest of what you posted looks fine.

If you want to change your c# code to look like this, it should work.

WebSocketServer server = new WebSocketServer( "localhost", 8080 );
server.Start();

MLM Software said...

The blog was absolutely fantastic! Lots of great information and
inspiration, both of which we all need!b Keep 'em coming... you all do
such a great job at such Concepts... can't tell you how much I, for
one appreciate all you do!

MLM Software

Anonymous said...

Hi - have not looked into the code or specification yet. Just wanted to know if the server could initiate the link, with the client doing the listening???
Ta - Gerard

jkworth said...

Anonymous, I do not think so. I do not recall the spec indicating that the connection could be started from the server. Once connected though, the server can send anything it wants without it having to be as a response to the client.

Anonymous said...

Hey there,
Do you know if two of these could be used on the same port, but with different sub-paths? For example, localhost:1234:/service1 and localhost:1234:/service2. Thanks.

jkworth said...

I do not know but it does handle multiple connections at one time so not sure why you would want to. If you are wanting to be able to have each serving a specific purpose I would just use some init comm to identifiy the clients needs and the servers logic and diverge from there.

Hope that helps.

jkworth said...

I do not know. I had hoped that MS would have put something in .NET by now. I mean how long do they plan to hold out on this one.

contact microsoft said...

"Thanks for Sharing!
If you have any issues while using microsoftoffice . dial our Microsoft Customer Service Phone Number +1-877-353-1149. You will get instant support from our microsoft team
https://goo.gl/emE4PB"