[ACCEPTED]-Websocket server: onopen function on the web socket is never called-websocket

Accepted answer
Score: 49

Probably it's an encoding issue. Here's 5 a working C# server I wrote:

class Program
{
    static void Main(string[] args)
    {
        var listener = new TcpListener(IPAddress.Loopback, 8181);
        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:8080");
            writer.WriteLine("WebSocket-Location: ws://localhost:8181/websession");
            writer.WriteLine("");
        }
        listener.Stop();
    }
}

And the corresponding 4 client hosted on localhost:8080:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <script type="text/javascript">
      var socket = new WebSocket('ws://localhost:8181/websession');
      socket.onopen = function() {
        alert('handshake successfully established. May send data now...');
      };
      socket.onclose = function() {
        alert('connection closed');
      };
    </script>
  </head>
  <body>
  </body>
</html>

This example only establishes 3 the handshake. You will need to tweak the 2 server in order to continue accepting data 1 once the handshake has been established.

Score: 24

Please use UTF8 encoding to send text message.

There 3 is an open source websocket server which 2 is implemented by C#, you can use it directly.

http://superwebsocket.codeplex.com/

It's 1 my open source project!

Score: 3

The .NET Framework 4.5 introduces support 12 for WebSockets in Windows Communication 11 Foundation. WebSockets is an efficient, standards-based technology 10 that enables bidirectional communication 9 over the standard HTTP ports 80 and 443. The 8 use of the standard HTTP ports allow WebSockets 7 to communicate across the web through intermediaries. Two new 6 standard bindings have been added to support 5 communication over a WebSocket transport. NetHttpBinding 4 and NetHttpsBinding. WebSockets-specific 3 settings can be configured on the HttpTransportBinding 2 element by accessing the WebSocketSettings property.

I 1 think it still uses SOAP though

http://msdn.microsoft.com/en-us/library/hh674271(v=vs.110).aspx

More Related questions