状态200上的WebSocket错误是什么?

编程入门 行业动态 更新时间:2024-10-06 22:19:04
本文介绍了状态200上的WebSocket错误是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经购买了WebSocket模块并将其安装在WAMP环境中.我也有一个PHP脚本,可以在正确的位置生成IPC文件,并永远循环以侦听事件.但是,使用此客户端代码:

I have bought a WebSocket module and installed it on my WAMP environment. I also have a PHP script which generates the IPC file at the correct location and loops forever to listen to events. However, using this client-side code:

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); }

触发连接后,我的请求失败,并出现以下错误:

When a connection is triggered, my request fails with the following error:

WebSocket连接到 'ws://websocket-example/WebSocket/websocket-example-websocket.rp' 失败:WebSocket握手期间出错:意外的响应代码: 200

WebSocket connection to 'ws://websocket-example/WebSocket/websocket-example-websocket.rp' failed: Error during WebSocket handshake: Unexpected response code: 200

和websocket保持null.我绝对不理解此错误,因为200似乎是可以的状态,但是我的请求仍然失败. IPC文件是在服务器启动后生成的,位于正确的位置,并且执行脚本的用户具有请求该文件的必要特权.这种现象的原因是什么,我该如何解决?

and websocket remains null. I absolutely do not understand this error, as 200 seems to be an OK status, but still, my request fails. The IPC file was generated after server start, is at the correct location and the user executing the script has the necessary privileges to request the file. What is the reason of this behavior and how can I fix it?

推荐答案

在WebSocket连接上,您需要101 Switching Protocols状态响应. 获得200状态响应,可能意味着该请求未到达您的WebSocket处理程序.

On a WebSocket connection you want a 101 Switching Protocols status response. Getting a 200 status response, probably means that the request didn't reach your WebSocket Handler.

我还将研究开发工具,并检查响应中是否包含任何WebSocket握手标头. 如果有的话,我认为这是该模块的问题.否则,可能是您的配置.

I would also look into the dev-tools, and check if the response has any WebSocket handshake headers. If it has, I would assume it's a problem with the module. Otherwise it's probably your configuration.

更多推荐

状态200上的WebSocket错误是什么?

本文发布于:2023-10-10 18:20:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   错误   WebSocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!