Мой код js:
let socket = new WebSocket("ws://"+location.host+"/ws");
console.log("Attempting Connection...");
socket.onopen = () => {
console.log("Successfully Connected");
socket.send("Hi From the Client!")
};
socket.onclose = event => {
console.log("Socket Closed Connection: ", event);
socket.send("Client Closed!")
};
socket.onerror = error => {
console.log("Socket Error: ", error);
};
Мой код go:
package main
import (
"log"
"net/http"
)
func HelloServer(w http.ResponseWriter, req *http.Request) {
log.Println("Connected: ")
defer log.Println("Disonnected: ")
_, err := w.Write([]byte("Hello, world!"))
if err != nil {
log.Println("error: " , err.Error())
}
}
func main() {
http.HandleFunc("/ws", HelloServer)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Println("error: " , err)
}
}
Nginx:
location /ws {
proxy_pass http://127.0.0.1:8080/ws;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Что не так?
Ошибка:
WebSocket connection to 'ws://*/ws' failed: Error during WebSocket handshake: Unexpected response code: 404