func websocketHandler(w http.ResponseWriter, req *http.Request) {

	limit := 1 << 30
	s := req.FormValue("limit")
	if len(s) > 0 {
		var err error
		limit, err = strconv.Atoi(s)
		if err != nil {
			log.Println(err)
			return
		}
	}

	// Perform Websocket upgrade.
	conn, err := upgrader.Upgrade(w, req, nil)
	if err != nil {
		log.Println("Upgrade Error: ", err)
		return
	}
	defer conn.Close()

	log.Println("WebSocket connection initiated.")

	for i := 0; i < limit; i++ {
		msgType, bytes, err := conn.ReadMessage()
		if err != nil {
			log.Println("Read Error: ", err)
			break
		}

		// We don't recognize any message that is not text.
		if msg := string(bytes); msgType != websocket.TextMessage {
			log.Println("Non-text message received, ignoring.")
			continue
		} else {
			log.Println("Received:", msg)
		}

		log.Println("Sending: same message as echo back.")
		err = conn.WriteMessage(websocket.TextMessage, bytes)
		if err != nil {
			log.Println("Write Error: ", err)
			break
		}
	}
	log.Println("WebSocket connection terminated.")
}
// websocketHandler echo back limited number of websocket messages and then close connection.
// Limit is accepted via 'limit' parameter.
websocketHandler echo back limited number of websocket messages and then close connection.
Limit is accepted via 'limit' parameter.
// websocketHandler echo back limited number of websocket messages and then close connection . // Limit is accepted via ' limit ' parameter .
func websocketHandler(w http.ResponseWriter, req *http.Request) {
	limit := 1 << 30
	s := req.FormValue("limit")
	if len(s) > 0 {
		var err error
		limit, err = strconv.Atoi(s)
		if err != nil {
			log.Println(err)
			return
		}
	}
	conn, err := upgrader.Upgrade(w, req, nil)
	if err != nil {
		log.Println("Upgrade Error: ", err)
		return
	}
	defer conn.Close()
	log.Println("WebSocket connection initiated.")
	for i := 0; i < limit; i++ {
		msgType, bytes, err := conn.ReadMessage()
		if err != nil {
			log.Println("Read Error: ", err)
			break
		}
		if msg := string(bytes); msgType != websocket.TextMessage {
			log.Println("Non-text message received, ignoring.")
			continue
		} else {
			log.Println("Received:", msg)
		}
		log.Println("Sending: same message as echo back.")
		err = conn.WriteMessage(websocket.TextMessage, bytes)
		if err != nil {
			log.Println("Write Error: ", err)
			break
		}
	}
	log.Println("WebSocket connection terminated.")
}
func websocketHandler ( w http . ResponseWriter , req * http . Request ) { limit := 1 << 30 s := req . FormValue ( " " ) if len ( s ) > 0 { var err error limit , err = strconv . Atoi ( s ) if err != nil { log . Println ( err ) return } } conn , err := upgrader . Upgrade ( w , req , nil ) if err != nil { log . Println ( " " , err ) return } defer conn . Close ( ) log . Println ( " " ) for i := 0 ; i < limit ; i ++ { msgType , bytes , err := conn . ReadMessage ( ) if err != nil { log . Println ( " " , err ) break } if msg := string ( bytes ) ; msgType != websocket . TextMessage { log . Println ( " " ) continue } else { log . Println ( " " , msg ) } log . Println ( " " ) err = conn . WriteMessage ( websocket . TextMessage , bytes ) if err != nil { log . Println ( " " , err ) break } } log . Println ( " " ) }
websocketHandler echo back limited number of websocket messages and then close connection.
websocketHandler echo back limited number of websocket messages and then close connection .
// Perform Websocket upgrade. // We don't recognize any message that is not text.