私は、クライアントとサーバ間の通信にソケットを使用してC#(クライアント/サーバ)でアプリケーションを開発しました。今、私はアンドロイドのアプリケーションを(development guide about javascriptに基づいて)javascriptを使って作成したいのですが、サーバーをC#で作成したままにしておきたいと思います。ここでは、コードがどのように見えるかです:javascriptとC#サーバの通信
サーバ側:
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"));
補助がある:
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;
}
私はCシャープサーバーに接続WebSocketを持つJavaScriptでクライアントを作成しました。問題は、ハンドシェイクの後に私の接続が閉じて、私は理由を知らないということです。私が得る唯一のエラーは、 "認識できないフレームのオペコード:7"です。私はGoogle Chrome 16.0.912.75を使用しています。
ステップバイステップ:利用可能
- のWebSocketは、ブラウザがキーと認識し
- 生成され、ブラウザに送信されます。 onopenメソッドが実行されました
この後エラーが発生します
var ws; $(document).ready(function() { // test if the browser supports web sockets 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 to send data on the web socket function ws_send(str) { try { ws.send(str); } 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! Big Grin | :-D }; ws.onmessage = function (evt) { debug(evt.data, '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() { debug("\n"); connect($('#host').val()); }); // the user clicked the send button $('#console_send').click(function() { ws_send($('#console_input').val()); }); $('#console_input').keyup(function (e) { if(e.keyCode == 13) // enter is pressed ws_send($('#console_input').val()); }); });
詳細情報が必要な場合はお返事ください。私は今このopcode:4号のように4hのようにgoogleを探しています。
これについてはhttps://github.com/SignalR/SignalRをご覧ください。これは、javascriptを使ってリアルタイムアプリケーションをより管理しやすくします。 –
クライアント(ブラウザ)に「Unrecognized frame」というエラーがありますか?もしそうなら、上記のコードを投稿したハンドシェークが成功した後、サーバーがデータを送信している可能性はありますか? wiresharkを実行すると、ハンドシェイク後に通信があるかどうかを確認できます。 – simonc