我在JavaScript中创建的WebSockets交流急剧服务器连接到客户端。问题是,握手后,我的连接关闭,我不知道为什么。我得到的唯一错误是:"无法识别的帧操作码:7"。我用谷歌Chrome 16.0.912.75。
一步一步:
- WebSocket的可用
- 服务器接收的握手请求
- 节WebSocket的,接受的是生成和发送到浏览器
- 浏览器可识别的关键OnOpen方法执行
。 - 这之后我得到的错误
在我的服务器,我有这个:
...其中辅助生成功能:
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"));
{C}
在客户端看起来像这样:
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!
};
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());
});
});
如果您需要了解更多信息,请回复。我正在寻找像4H谷歌现在在这个操作码:7。问题
谢谢你的帮助。