构造 'WebSocket' 失败:URL '{{.}}' 无效
Failed to construct 'WebSocket': The URL '{{.}}' is invalid
gorilla/websocket
例子
在这个例子中:
WebSocket 创建者:
ws = new WebSocket("{{.}}");
示例运行良好。
问题
但是,我想在另一个 JS 代码中使用相同的代码,例如:
var ws;
ws = new WebSocket("{{.}}");
ws.onopen = function(evt) {
console.log("OPEN SOCKET");
}
ws.onclose = function(evt) {
console.log("CLOSE SOCKET");
ws = null;
}
ws.onmessage = function(evt) {
console.log("RESPONSE SOCKET: RECEIVED");
}
ws.onerror = function(evt) {
console.log("ERROR: " + evt.data);
}
ws.send(positions);
ws.close();
我收到此错误:
Uncaught (in promise) DOMException: Failed to construct 'WebSocket': The URL '{{.}}' is invalid.
at AddObjectCommand.execute
我这样更改了 WebSocket:
ws = new WebSocket("ws://127.0.0.1:8080/echo");
但我仍然收到错误消息:
WebSocket connection to 'ws://127.0.0.1:8080/echo' failed: Error during WebSocket handshake: Unexpected response code: 403
我不知道我做错了什么。我错过了什么?
修复403
此建议解决了 403
错误:
https://github.com/gorilla/websocket/issues/367#issuecomment-375971418
通过添加:
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
之前:
c, err := upgrader.Upgrade(w, r, nil)
当然,相信所有来源并不安全。只是临时修复。
又一个错误
但是抛出另一个错误:
Uncaught (in promise) DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
错误:
Uncaught (in promise) DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
通过 WebSocket 发送数据,通过其 .onopen
回调解决:
var positions = ...
var ws;
ws = new WebSocket("ws://127.0.0.1:8080/echo");
ws.onopen = function(evt) {
console.log("OPEN SOCKET");
console.log("SEND: START");
ws.send(positions);
ws.close();
}
gorilla/websocket
例子
在这个例子中:
WebSocket 创建者:
ws = new WebSocket("{{.}}");
示例运行良好。
问题
但是,我想在另一个 JS 代码中使用相同的代码,例如:
var ws;
ws = new WebSocket("{{.}}");
ws.onopen = function(evt) {
console.log("OPEN SOCKET");
}
ws.onclose = function(evt) {
console.log("CLOSE SOCKET");
ws = null;
}
ws.onmessage = function(evt) {
console.log("RESPONSE SOCKET: RECEIVED");
}
ws.onerror = function(evt) {
console.log("ERROR: " + evt.data);
}
ws.send(positions);
ws.close();
我收到此错误:
Uncaught (in promise) DOMException: Failed to construct 'WebSocket': The URL '{{.}}' is invalid. at AddObjectCommand.execute
我这样更改了 WebSocket:
ws = new WebSocket("ws://127.0.0.1:8080/echo");
但我仍然收到错误消息:
WebSocket connection to 'ws://127.0.0.1:8080/echo' failed: Error during WebSocket handshake: Unexpected response code: 403
我不知道我做错了什么。我错过了什么?
修复403
此建议解决了 403
错误:
https://github.com/gorilla/websocket/issues/367#issuecomment-375971418
通过添加:
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
之前:
c, err := upgrader.Upgrade(w, r, nil)
当然,相信所有来源并不安全。只是临时修复。
又一个错误
但是抛出另一个错误:
Uncaught (in promise) DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
错误:
Uncaught (in promise) DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
通过 WebSocket 发送数据,通过其 .onopen
回调解决:
var positions = ...
var ws;
ws = new WebSocket("ws://127.0.0.1:8080/echo");
ws.onopen = function(evt) {
console.log("OPEN SOCKET");
console.log("SEND: START");
ws.send(positions);
ws.close();
}