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
1 changed files with 28 additions and 7 deletions

35
main.go
View File

@ -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)
}
}