Пример #1
0
func NewNode(config *ServerConfig) (node *Node) {
	router := NewRouter(config)
	socketServer := &ws.Server{
		Config: ws.Config{
			Version: ws.ProtocolVersionHybi13,
		},
		Handler: func(socket *ws.Conn) {
			conn, err := NewConn(router, socket)
			if err != nil {
				socket.Close()
				return
			}
			defer conn.Close()
			// Start the state machine.
			_ = conn.Receive()
		},
	}
	serveMux := http.NewServeMux()
	serveMux.HandleFunc("/", func(responseWriter http.ResponseWriter, request *http.Request) {
		if ok := util.RequireMethod("GET", responseWriter, request); !ok {
			return
		}
		socketServer.ServeHTTP(responseWriter, request)
	})
	node = &Node{
		Router: router,
		Server: &http.Server{
			Addr:    config.Addr,
			Handler: serveMux,
		},
	}
	return
}
Пример #2
0
func (server *Server) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
	if ok := util.RequireMethod("GET", responseWriter, request); !ok {
		return
	}
	server.SocketServer.ServeHTTP(responseWriter, request)
}