works with tcp connection

try nc localhost 8080 to see progress
This commit is contained in:
Derek McQuay 2015-11-30 21:52:08 -08:00
parent b567834227
commit efcf78bec1

35
main.go
View File

@ -2,8 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"io"
"log"
"math/rand" "math/rand"
"net/http" "net"
"sync" "sync"
"time" "time"
) )
@ -73,8 +75,20 @@ var q = queue{
} }
func main() { func main() {
http.HandleFunc("/", handler) listener, err := net.Listen("tcp", "localhost:8080")
go http.ListenAndServe(":8080", nil) 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") fmt.Println("here")
load(&q) load(&q)
printer(&q) printer(&q)
@ -87,8 +101,15 @@ func main() {
} }
} }
func handler(w http.ResponseWriter, req *http.Request) { func handleConn(c net.Conn) {
mu.Lock() defer c.Close()
fmt.Fprintf(w, fmt.Sprintf("%v", q)) for {
mu.Unlock() mu.Lock()
_, err := io.WriteString(c, fmt.Sprintf("%v\n", q))
mu.Unlock()
if err != nil {
return
}
time.Sleep(time.Millisecond * 100)
}
} }