[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);
        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");

And the corresponding 4 client hosted on localhost:8080:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml">
    <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');

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.


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


More Related questions