So... ASP.NET MVC and WebSockets?

bevacqua picture bevacqua · Jul 11, 2012 · Viewed 24.5k times · Source

I have an application in MVC 3 and I'm looking to add WebSockets (with fallback to Comet) to it.

I've researched a bit and I found out the Comet part is pretty straightforward and I'd much rather do it myself. Just AsyncControllers and a pretty plain bit of js is all that's required to handle those long-lived ajax requests.

Now, in the case of WebSocket, things start to get dirty. I've looked at a few libraries, but they mostly seem to set up a web server (therefore needing another host, or port) of their own, and listen to ws protocol requests there. This is the case for instance for SuperWebSocket, which seemed nice at first, but had this "I'm a web server" issue (which is perfectly fine of course, but I'd rather avoid).

Then I looked at "PingIt" or something like that, I can't find the link now.., I do have the source on another computer though. This one DOES run on an endpoint in mvc, but I didn't quite like the way in which it handles things, like it takes an IDisposable object and through reflector it creates a javascript piece that gets rendered in the client, which is very polluted with their library's name, which I've really no interest in, plus it felt like a lot of it was being thrown in against what I might wish, which kind of goes against my view on how a page should be rendered like (specially now that I'm working on MVC which pretty much means I can code clean, unobtrusive html pages).

Basically what I want is my endpoints to be something like:

domain.com/rt/comet
domain.com/rt/socket

rather than

domain.com/rt/comet
domain.com:81/

So: is it possible to receive websocket connections (and do handshaking and whatever needs to be done) on an endpoint within an asp.net MVC application's controller, rather than setting up a tcplistener somewhere?

This would also help me keep my comet code a little closer to my websocket code

I should say I'm seriously new to the whole comet/websockets thing, so I don't really know much (or any) of the protocol, I do understand how to make comet work, but not so much in websockets, though I've read and understood the basics to get the gist of it.

Also: please let me know if what I'm asking is way off

Answer

Paul picture Paul · Jul 11, 2012