状态 200 上的 WebSocket 错误是什么?
What is the WebSocket error on status 200?
我已经购买了一个 WebSocket 模块并将其安装在我的 WAMP 环境中。我还有一个 PHP 脚本,它在正确的位置生成 IPC 文件并永远循环以侦听事件。但是,使用此客户端代码:
var websocket = null;
var connect = function() {
var button = document.getElementById('connect-button');
// This function is a convenience function, to set the content
// of the response display
var setResponse = function(text) {
var element = document.getElementById('response');
element.innerHTML = text;
}
// A message telling the user we started connecting is always
// useful
button.innerHTML = 'Connecting ...';
// The file name `demo.ws' could in principle help in having multiple
// websockets at a single domain name.
//
// It's not implemented right now, but it can be if it's needed and
// it's not too hard.
//var url = "ws://websocket-example/websocket-example/websocket-example-websocket.rp";
var url = 'ws://' + window.location.hostname + '/WebSocket/websocket-example-websocket.rp';
// Create the websocket connection now
websocket = new WebSocket(url, 'standard');
// Install the handlers, the On Open handler is triggered
// immediately after the conection has been established
// and a successful handshake
websocket.onopen = function(event) {
// Update the connection status indicator
var element = document.getElementById('connection-status');
var input = document.getElementById('input');
element.innerHTML = 'Connected Now';
element.setAttribute('class', 'online');
// Update the button, and install a new handler to allow
// closing the websocket connection
button.innerHTML = 'Close';
button.onclick = function() {
websocket.close();
}
input.focus();
input.value = '';
}
// On close and on error handler, this is a simple demo
// hence the simplistic approach. Ideally this should be
// two separate functions
websocket.onclose =
websocket.onerror = function(event) {
// Update the connection status indicator
var element = document.getElementById('connection-status');
var input = document.getElementById('input');
element.innerHTML = 'Offline';
element.setAttribute('class', 'offline');
// Update button click handler, to reconnect if requested
button.innerHTML = 'Connect';
button.onclick = connect;
input.value = '';
// Clear the response text
setResponse('');
// Reset the websocket global variable
websocket = null;
}
// On message handler, triggered when a message is received
websocket.onmessage = function(event) {
// Set the response text
setResponse(event.data);
}
}
var send = function(message) {
// Send a message to the server but check that the websocket
// was connected first
if (websocket === null)
return;
// It's ok so, send the message now
websocket.send(message);
}
触发连接后,我的请求失败并出现以下错误:
WebSocket connection to
'ws://websocket-example/WebSocket/websocket-example-websocket.rp'
failed: Error during WebSocket handshake: Unexpected response code:
200
和 websocket
仍然是 null
。我完全不明白这个错误,因为 200 似乎是一个 OK 状态,但我的请求仍然失败。 IPC 文件是在服务器启动后生成的,位于正确的位置,执行脚本的用户具有请求该文件的必要权限。这种行为的原因是什么?我该如何解决?
这样的响应意味着位于 URL 的远程资源没有监听或者至少不能响应 websocket 请求。
检查您尝试访问的远程服务。如果您对服务器进行了编码,post 您的来源
在 WebSocket 连接上,您需要 101 Switching Protocols
状态响应。
获得 200
状态响应,可能意味着请求未到达您的 WebSocket 处理程序。
我还会查看 dev-tools,并检查响应是否有任何 WebSocket 握手 headers。
如果有,我会认为这是模块的问题。否则它可能是你的配置。
我已经购买了一个 WebSocket 模块并将其安装在我的 WAMP 环境中。我还有一个 PHP 脚本,它在正确的位置生成 IPC 文件并永远循环以侦听事件。但是,使用此客户端代码:
var websocket = null;
var connect = function() {
var button = document.getElementById('connect-button');
// This function is a convenience function, to set the content
// of the response display
var setResponse = function(text) {
var element = document.getElementById('response');
element.innerHTML = text;
}
// A message telling the user we started connecting is always
// useful
button.innerHTML = 'Connecting ...';
// The file name `demo.ws' could in principle help in having multiple
// websockets at a single domain name.
//
// It's not implemented right now, but it can be if it's needed and
// it's not too hard.
//var url = "ws://websocket-example/websocket-example/websocket-example-websocket.rp";
var url = 'ws://' + window.location.hostname + '/WebSocket/websocket-example-websocket.rp';
// Create the websocket connection now
websocket = new WebSocket(url, 'standard');
// Install the handlers, the On Open handler is triggered
// immediately after the conection has been established
// and a successful handshake
websocket.onopen = function(event) {
// Update the connection status indicator
var element = document.getElementById('connection-status');
var input = document.getElementById('input');
element.innerHTML = 'Connected Now';
element.setAttribute('class', 'online');
// Update the button, and install a new handler to allow
// closing the websocket connection
button.innerHTML = 'Close';
button.onclick = function() {
websocket.close();
}
input.focus();
input.value = '';
}
// On close and on error handler, this is a simple demo
// hence the simplistic approach. Ideally this should be
// two separate functions
websocket.onclose =
websocket.onerror = function(event) {
// Update the connection status indicator
var element = document.getElementById('connection-status');
var input = document.getElementById('input');
element.innerHTML = 'Offline';
element.setAttribute('class', 'offline');
// Update button click handler, to reconnect if requested
button.innerHTML = 'Connect';
button.onclick = connect;
input.value = '';
// Clear the response text
setResponse('');
// Reset the websocket global variable
websocket = null;
}
// On message handler, triggered when a message is received
websocket.onmessage = function(event) {
// Set the response text
setResponse(event.data);
}
}
var send = function(message) {
// Send a message to the server but check that the websocket
// was connected first
if (websocket === null)
return;
// It's ok so, send the message now
websocket.send(message);
}
触发连接后,我的请求失败并出现以下错误:
WebSocket connection to 'ws://websocket-example/WebSocket/websocket-example-websocket.rp' failed: Error during WebSocket handshake: Unexpected response code: 200
和 websocket
仍然是 null
。我完全不明白这个错误,因为 200 似乎是一个 OK 状态,但我的请求仍然失败。 IPC 文件是在服务器启动后生成的,位于正确的位置,执行脚本的用户具有请求该文件的必要权限。这种行为的原因是什么?我该如何解决?
这样的响应意味着位于 URL 的远程资源没有监听或者至少不能响应 websocket 请求。 检查您尝试访问的远程服务。如果您对服务器进行了编码,post 您的来源
在 WebSocket 连接上,您需要 101 Switching Protocols
状态响应。
获得 200
状态响应,可能意味着请求未到达您的 WebSocket 处理程序。
我还会查看 dev-tools,并检查响应是否有任何 WebSocket 握手 headers。
如果有,我会认为这是模块的问题。否则它可能是你的配置。