Click here to Skip to main content
12,406,687 members (56,872 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# chrome WebSockets
I created a client in javascript with websockets that connects to a c sharp server. The problem is that after the handshake my connection closes and I don't know why. The only error I get is: "Unrecognized frame opcode: 7". I use Google Chrome 16.0.912.75.

Step by step:
- WebSocket available
- server receives the handshake request
- Sec-WebSocket-Accept is generated and sent to the browser
- the browser recognizes the key and the .onopen method is executed
- after this I get the error

In my server I have this:
connection.Send(Encoding.ASCII.GetBytes("HTTP/1.1 101 Switching Protocols\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + aux));
... where aux is generated by this function:
public static String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
            const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            String secWebSocketAccept = String.Empty;
            // 1. Combine the request Sec-WebSocket-Key with magic key.
            String ret = secWebSocketKey + MagicKEY;
            Console.WriteLine("-   " + ret + "   -");
            // 2. Compute the SHA1 hash
            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
            // 3. Base64 encode the hash
            secWebSocketAccept = Convert.ToBase64String(sha1Hash);
            return secWebSocketAccept;

The client side looks like this:
var ws;
$(document).ready(function () {
    // test if the browser supports web sockets
    if ("WebSocket" in window) {
        debug("Browser supports web sockets!", 'success');
    } else {
        debug("Browser does not support web sockets", 'error');
    // function to send data on the web socket
    function ws_send(str) {
        try {
        } catch (err) {
            debug(err, 'error');
    // connect to the specified host
    function connect(host) {
        debug("Connecting to " + host + " ...");
        try {
            ws = new WebSocket(host); // create the web socket
        } catch (err) {
            debug(err, 'error');
        $('#host_connect').attr('disabled', true); // disable the 'reconnect' button

        ws.onopen = function () {
            debug("connected... ", 'success'); // we are in! :D
        ws.onmessage = function (evt) {
            debug(, 'response'); // we got some data - show it omg!!
        ws.onclose = function () {
            debug("Socket closed!", 'error'); // the socket was closed (this could be an error or simply that there is no server)
            $('#host_connect').attr('disabled', false); // re-enable the 'reconnect button
    // function to display stuff, the second parameter is the class of the <p> (used for styling)
    function debug(msg, type) {
        $("#console").append('<p class="' + (type || '') + '">' + msg + '</p>');
    // the user clicked to 'reconnect' button
    $('#host_connect').click(function () {
    // the user clicked the send button
    $('#console_send').click(function () {
    $('#console_input').keyup(function (e) {
        if(e.keyCode == 13) // enter is pressed

If you need more information please reply. I am searching google for like 4h now on this opcode: 7 issue.

Thank you for your help.
Posted 11-Jan-12 3:45am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It's just a guess, but in your server, you are using Encoding.ASCII, and in your ComputeWebSocketHandshakeSecurityHash09 function you are using Encoding.UTF8.

If I were you, I would first use rather ASCII, rather UTF-8, but not a mix of both.

Hope this helps.
Lucian Adrian Rosu 11-Jan-12 12:48pm
I have tried it and it seems it does not work.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I found what I was looking for. Here is a link to WebSockets in c sharp:[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.160730.1 | Last Updated 11 Jan 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100