works with tcp connection
try nc localhost 8080 to see progress
This commit is contained in:
parent
b567834227
commit
efcf78bec1
31
main.go
31
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) {
|
||||
func handleConn(c net.Conn) {
|
||||
defer c.Close()
|
||||
for {
|
||||
mu.Lock()
|
||||
fmt.Fprintf(w, fmt.Sprintf("%v", q))
|
||||
_, err := io.WriteString(c, fmt.Sprintf("%v\n", q))
|
||||
mu.Unlock()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user