Hello,
I'm trying to test a websocket server in C#.
I made the handshake allright, the server thinks the client is connected, but however in the client, it never executes the onopen function. It just ignores.
Client Handshake:
GET /test HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8181
Sec-WebSocket-Origin: http://localhost:8080
Sec-WebSocket-Key: WSPssvnny/8wcttUvZ0KvA==
Sec-WebSocket-Version: 8
Server Handshake:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: RN219+qVLgOwA+oRF4NG9aweYCQ=
Sec-WebScoket-Protocol: chat
Someone can tells me if there is anything wrong in the handshake, or why it doesnt execute the onopen funcion????
Thanks.
Edit: I'm sorry i'll add the code
this is to get the accept:
public static String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
{
const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
String secWebSocketAccept = String.Empty;
String ret = secWebSocketKey + MagicKEY;
SHA1 sha = new SHA1CryptoServiceProvider();
byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
secWebSocketAccept = Convert.ToBase64String(sha1Hash);
return secWebSocketAccept;
}
and to send it
string handshakeTeste = "Sec-WebSocket-Accept: " + ComputeWebSocketHandshakeSecurityHash09(key);
Handshake += handshakeTeste + Environment.NewLine + "Sec-WebSocket-Protocol: chat";
ConnectionSocket.BeginSend(HandshakeText, 0, HandshakeText.Length, 0, HandshakeFinished, null);
The server executes the "HandshakeFinished" method.
and the client code:
function ToggleConnectionClicked() {
if (SocketCreated && (ws.readyState == 0 || ws.readyState == 1)) {
ws.close();
} else {
Log("Trying to connect to the server ...");
try {
ws = new WebSocket("ws://" + document.getElementById("Connection").value);
SocketCreated = true;
} catch (ex) {
Log(ex, "ERROR");
return;
}
document.getElementById("ToggleConnection").innerHTML = "Disconnect";
ws.onopen = function () {
alert("Socket has been opened!");
}
ws.onmessage = WSonMessage;
ws.onclose = WSonClose;
ws.onerror = WSonError;
}
};