Browse Source

works with tcp connection

try nc localhost 8080 to see progress
master
Derek McQuay 7 years ago
parent
commit
efcf78bec1
  1. 35
      main.go

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

Loading…
Cancel
Save