WebSocket server large json

Hey there, I’m trying to implement a web socket server in GO, and I got the following example;

package main

import (
	"fmt"
	"log"
	"net/http"

	"golang.org/x/net/websocket"
)

func echoHandler(ws *websocket.Conn) {
	msg := make([]byte, 512)
	n, err := ws.Read(msg)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Receive: %s\n", msg[:n])

	m, err := ws.Write(msg[:n])
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Send: %s\n", msg[:m])
}

func main() {
	http.Handle("/echo", websocket.Handler(echoHandler))
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic("ListenAndServe: " + err.Error())
	}
}

How can I send to the client for example a very large json?

Do you know how to custruct a JSON response? Try to build and return a small and simple JSON. Then you can try large and complicated data.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.