3

Client:

 <script type="text/javascript"> var socket = new WebSocket('ws://localhost:8183/websession'); socket.onopen = function () { alert('handshake successfully established. May send data now...'); }; socket.onclose = function () { alert('connection closed'); }; </script> 

server:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using System.IO; class Program { static void Main(string[] args) { var listener = new TcpListener(IPAddress.Loopback, 8183); listener.Start(); using (var client = listener.AcceptTcpClient()) using (var stream = client.GetStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) { writer.WriteLine("HTTP/1.1 101 Web Socket Protocol Handshake"); writer.WriteLine("Upgrade: WebSocket"); writer.WriteLine("Connection: Upgrade"); writer.WriteLine("WebSocket-Origin: http://localhost:8092"); writer.WriteLine("WebSocket-Location: ws://localhost:8183/websession"); writer.WriteLine(""); } listener.Stop(); } } 

the problem is that the connection is never established and the onopen function is never called what may be the problem ?

4
  • what do you suggest ? what should i use ? Commented Nov 18, 2013 at 8:00
  • @Damien_The_Unbeliever that's not true; a web-socket is absolutely a TCP socket, and you absolutely can implement web-sockets over TcpListener. Personally I'd use a naked Socket / Stream, though. I know this because I wrote the web-socket server that we use at Stack Exchange. which is ... kinda busy. Commented Nov 18, 2013 at 8:04
  • so @MarcGravell can you please tell me why my code is not working ? Commented Nov 18, 2013 at 8:07
  • @Sora working on it ;p answer is in progress Commented Nov 18, 2013 at 8:07

1 Answer 1

3

Firstly, your "server" seems to be sending the "client" part of the handshake. Secondly, StreamReader and StreamWriter will not help you much; frankly, you would do well just to use the Stream.

Now, the actual handshake is complicated; both client and server need to prove to each-other than they are actually talking web-sockets, and not HTTP. The exact nature of this depends on the version of the protocol you are trying to support (or multiple protocols), but the server is required to perform a maths calculation based on the headers that the client sends; the server sends the answer back in its headers, and this checks that they are talking the same language. Note that this is not a security step: it is a sanity protection step. This is all described in the specification, but an example request / response pair is (from that document):

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 

And:

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat 

After which, it drops from HTTP into web-socket protocol, which is binary and unrelated to HTTP. For info, Sec-WebSocket-Accept is the result of the maths calculation, based on the client's Sec-WebSocket-Key.

The next bit of code that you will need is frame support. Again - see the specification. Also keep in mind that this specification is just version 13. If you want to support all clients, you might need to check the older specifications too (there are some subtle tweaks, except for the original original protocol, which was radically different and will require completely different code).

Before writing all this, you might want to look into whether a pre-existing library would be more practical. Windows 8 (and similar versions of server) include web-socket supprt direct in HTTP.SYS, with full support in .NET.

Sign up to request clarification or add additional context in comments.

3 Comments

i read your reply and i saw the specification link but i am still can't figure out how should i fix my code to accomplish the connection
@Sora by implementing the web-sockets protocol and sending the correct response headers; and then implementing the frames; this is not trivial code. I can't give you an estimate on "how long" because a: I don't know how familiar you are with binary protocols, and b: I don't know many clients this server needs to support (which can change the design significantly). But at a minimum this is going to be a solid day's coding. Hence I can't really include all that in a reply here. Except to say: please consider using an existing library unless you really really need it done manually.
thank you so much for your time and hard work and thank you for your reply i will take what you said under consideration by reading more about websockets and how to implement them

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.