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));
connection.Send(Encoding.ASCII.GetBytes("\r\n\r\n"));
... 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;
String ret = secWebSocketKey + MagicKEY;
Console.WriteLine("- " + ret + " -");
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
secWebSocketAccept = Convert.ToBase64String(sha1Hash);
return secWebSocketAccept;
}
The client side looks like this:
var ws;
$(document).ready(function () {
if ("WebSocket" in window) {
debug("Browser supports web sockets!", 'success');
connect($('#host').val());
$('#console_send').removeAttr('disabled');
} else {
debug("Browser does not support web sockets", 'error');
};
function ws_send(str) {
try {
ws.send(str);
} catch (err) {
debug(err, 'error');
}
}
function connect(host) {
debug("Connecting to " + host + " ...");
try {
ws = new WebSocket(host);
} catch (err) {
debug(err, 'error');
}
$('#host_connect').attr('disabled', true);
ws.onopen = function () {
debug("connected... ", 'success');
};
ws.onmessage = function (evt) {
debug(evt.data, 'response');
};
ws.onclose = function () {
debug("Socket closed!", 'error');
$('#host_connect').attr('disabled', false);
};
};
function debug(msg, type) {
$("#console").append('<p class="' + (type || '') + '">' + msg + '</p>');
};
$('#host_connect').click(function () {
debug("\n");
connect($('#host').val());
});
$('#console_send').click(function () {
ws_send($('#console_input').val());
});
$('#console_input').keyup(function (e) {
if(e.keyCode == 13)
ws_send($('#console_input').val());
});
});
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.