diff --git a/main.go b/main.go index 11fe501..4685cee 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,10 @@ package main import ( "fmt" + "io" + "log" "math/rand" - "net/http" + "net" "sync" "time" ) @@ -73,8 +75,20 @@ var q = queue{ } func main() { - http.HandleFunc("/", handler) - go http.ListenAndServe(":8080", nil) + listener, err := net.Listen("tcp", "localhost:8080") + if err != nil { + log.Fatal(err) + } + go func() { + for { + conn, err := listener.Accept() + if err != nil { + log.Print(err) + continue + } + handleConn(conn) + } + }() fmt.Println("here") load(&q) printer(&q) @@ -87,8 +101,15 @@ func main() { } } -func handler(w http.ResponseWriter, req *http.Request) { - mu.Lock() - fmt.Fprintf(w, fmt.Sprintf("%v", q)) - mu.Unlock() +func handleConn(c net.Conn) { + defer c.Close() + for { + mu.Lock() + _, err := io.WriteString(c, fmt.Sprintf("%v\n", q)) + mu.Unlock() + if err != nil { + return + } + time.Sleep(time.Millisecond * 100) + } }