php editor Banana will introduce you to a method using the x/net library to implement a Websocket server that throws a 403 error. Websocket is a communication protocol that establishes a persistent connection between a client and a server, and a 403 error means that the server rejected the client's request. By using the x/net library, we can easily create a Websocket server and reject requests by throwing a 403 error when needed. This method is simple and effective, and is suitable for scenarios where requests need to be authorized or access restricted.
I am trying to implement a websocket server using thex/net/websocket
standard library.
My attempts so far are as follows:
package main import ( "fmt" "net/http" "golang.org/x/net/websocket" ) type Server struct { baseUri string connections map[string][]*websocket.Conn } func initServer(baseUri string) *Server { return &Server{ baseUri: baseUri, } } func (server *Server) handleConnections() { http.Handle("/ws", websocket.Handler(server.listenConnections)) http.ListenAndServe(":3000", nil) } func (server *Server) listenConnections(ws *websocket.Conn) { fmt.Println("New connection established") for { fmt.Println("FOO") } } func main() { server := initServer("/ws") server.handleConnections() }
When trying to connect tows://localhost:3000/ws
using multiple ws clients, I always get the same error:403-Forbidden
. I even tried the example from the official documentation but still get it. Am I missing something obvious? Like default port blocking or something like that?
Thank you in advance.
EDIT: You may need to use a different port to reproduce the issue. Using3000
in my example will just interrupt the execution of the program if it is not available.
Edit 2: You can use a client like websocat and executewebsocat 'ws://localhost:3000/ws'
to try to connect to the server
I gave up, but had good insights: If you're like me and are following Anthony GG'sWalkthrough of Creating a Websocket Server on Go from Scratch, don't. Video is outdated, and while it provides a good intuition on how to create videos, it's best (and no shame) to learn using gorilla's websocket library.
package main import ( "fmt" "net/http" "time" "github.com/gorilla/mux" "github.com/gorilla/websocket" ) var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, } type Server struct { baseUri string connections map[string][]*websocket.Conn router *mux.Router setup *http.Server } func initServer(baseUri string) *Server { router := mux.NewRouter() return &Server{ baseUri: baseUri, router: router, setup: &http.Server{ Handler: router, Addr: "127.0.0.1:8000", WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, }, } } func (server *Server) handleConnections() { server.router.HandleFunc("/ws/{var}", server.listenConnections) server.setup.ListenAndServe() } func (server *Server) listenConnections(w http.ResponseWriter, r *http.Request) { connection, err := upgrader.Upgrade(w, r, nil) if err != nil { fmt.Println(err) return } for { _, message, err := connection.ReadMessage() if err != nil { break } connection.WriteMessage(websocket.TextMessage, message) go messageHandler(message) } fmt.Println("Out of loop") } func messageHandler(message []byte) { fmt.Println(string(message)) } func main() { server := initServer("/ws") server.handleConnections() }
I also usedgorilla/mux
to use path parameters (not sure why the http handler can't). Notice how I changedhttp.Handle
tomux.Router.HandleFunc
. As user @Cerise pointed out in the comments, thex/net/websocket
package is not in the standard library, but just adding theOrigin
header didn't solve the original problem either.
Hopefully this saves some of the trouble others like me running into when learning Go.
The above is the detailed content of Websocket server implementation using x/net library trowing 403. For more information, please follow other related articles on the PHP Chinese website!