ws/cmd/gwstester/tester.go

69 lines
1.1 KiB
Go

package main
import (
"fmt"
"net"
"os"
"time"
"git.xdrm.io/gws/http/upgrade/request"
)
func main() {
/* (1) Create listening socket
---------------------------------------------------------*/
/* (1) Create socket */
lsock, err := net.Listen("tcp", ":4444")
if err != nil {
panic(err)
}
/* (2) Accept clients */
for {
sock, err := lsock.Accept()
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("Connection error: %s\n", err))
return
}
fmt.Printf("+ new client\n")
go manageClient(sock)
}
}
func manageClient(sock net.Conn) {
startTime := time.Now().UnixNano()
defer sock.Close()
for {
/* (1) Parse request */
req, errRq := request.Parse(sock)
if errRq != nil { fmt.Printf("Request parsing error: %s\n", errRq) }
fmt.Printf("%v\n", req)
/* (2) Build response */
res := req.BuildResponse()
/* (3) Write into socket */
written, err := res.Send(sock)
if err != nil {
fmt.Printf("WRITE ERROR: %s\n", err);
return
}
fmt.Printf("Written %d bytes\n", written)
break;
}
fmt.Printf("Elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
}