我正在try 实现WebSocket服务器using 100 standard library.
到目前为止,我的try 是这样的:
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()
}
try 使用多个WS客户端连接到ws://localhost:3000/ws
时,我总是收到相同的错误:A 403-Forbidden
.
我甚至试过了official doc's example米,但仍然能拿到.我是不是漏掉了什么明显的东西?类似于默认端口阻止或类似的?
提前谢谢您.
编辑:您可能希望使用不同的端口来重现该问题.在我的例子中使用3000
,如果不可用,只会中断程序的执行.
编辑2:您可以使用像websocat和DO websocat 'ws://localhost:3000/ws'
这样的客户端try 连接到服务器