Failed to construct 'WebSocket': The URL '//webSocket/sendAlarm' is invalid.
时间: 2024-03-29 21:40:56 浏览: 28
这个错误提示意味着你正在尝试使用一个无效的 WebSocket URL。WebSocket URL 必须包含协议(例如 http:// 或 https://)。在这个错误信息中,URL 似乎缺少协议部分。请确保你的 URL 是完整的,包含正确的协议(如 ws:// 或 wss://)。如果问题仍然存在,请检查你的代码并确保 WebSocket URL 是正确的。
相关问题
Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed
回答: 当出现"Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed"的错误时,这意味着在构建WebSocket对象时,URL的协议方案必须是'ws'或'wss',而不是'http'。\[1\]这个错误通常发生在尝试使用HTTP协议而不是WebSocket协议进行WebSocket连接时。WebSocket协议使用的是ws或wss协议方案,而不是http。\[3\]因此,要解决这个错误,你需要确保在构建WebSocket对象时使用正确的协议方案,即'ws'或'wss'。
#### 引用[.reference_title]
- *1* *2* [你还在使用 ajax 轮询吗?试试 WebSocket 让后端主动推送消息](https://blog.csdn.net/weixin_46784898/article/details/106440181)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [The WebSocket Protocol](https://blog.csdn.net/qiuchangyong/article/details/109633833)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
以上代码不对,提示Uncaught DOMException: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid. at XMLHttpRequest.xhr.onload (http://127.0.0.1:8848/virtualperson/virtualperson/demo1_transs/soundchat_response.html?chatname=yunconglong:541:20)
这个错误提示是因为在构建 WebSocket 对象时,传入的子协议参数是一个 JavaScript 对象而不是字符串。可以检查一下代码中传入 WebSocket 构造函数的参数是否正确,应该是一个字符串,例如:
```
var ws = new WebSocket('ws://localhost:8080', 'chat');
```
其中第二个参数是子协议字符串,可以根据实际情况进行修改。如果仍然出现错误,可以提供更多代码以便我帮助你解决问题。