VUE Front end to go server (http) and clients connected to go server (tcp) error

Viewed 27

I'm currently creating a go TCP server that handles file sharing between multiple go clients, that works fine. However, I'm also building a front end using vue.js showing some server stats like the number of users, bytes sent, etc.

The problem occurs when I include the 'http.ListenAndServe(":3000", nil)' function handles the requests from the front end of the server. Is it impossible to have a TCP and an HTTP server on the same go file?

If so, how can a link the three (frontend, go-server, clients)

Here is the code of the 'server.go'

func main() {

    // Create TCP server
    serverConnection, error := net.Listen("tcp", ":8085")

    // Check if an error occured
    // Note: because 'go' forces you to use each variable you declare, error
    // checking is not optional, and maybe that's good
    if error != nil {
        fmt.Println(error)
        return
    }

    // Create server Hub
    serverHb := newServerHub()

    // Close the server just before the program ends
    defer serverConnection.Close()

    // Handle Front End requests
    http.HandleFunc("/api/thumbnail", requestHandler)

    fs := http.FileServer(http.Dir("../../tcp-server-frontend/dist"))
    http.Handle("/", fs)

    fmt.Println("Server listening on port 3000")
    http.ListenAndServe(":3000", nil)

    // Each client sends data, that data is received in the server by a client struct
    // the client struct then sends the data, which is a request to a 'go' channel, which is similar to a queue

    // Somehow this for loop runs only when a new connection is detected
    for {

        // Accept a new connection if a request is made
        // serverConnection.Accept() blocks the for loop
        // until a connection is accepted, then it blocks the for loop again!
        connection, connectionError := serverConnection.Accept()

        // Check if an error occurred
        if connectionError != nil {
            fmt.Println("1: Woah, there's a mistake here :/")
            fmt.Println(connectionError)
            fmt.Println("1: Woah, there's a mistake here :/")
            // return
        }

        // Create new user
        var client *Client = newClient(connection, "Unregistered_User", serverHb)
        fmt.Println(client)

        // Add client to serverHub
        serverHb.addClient(client)
        serverHb.listClients()

        // go client.receiveFile()
        go client.handleClientRequest()

    }
}
0 Answers
Related