36 lines
797 B
Go
Raw Permalink Normal View History

2023-06-26 03:16:43 -05:00
package main
import (
"bufio"
"net/http"
"time"
"github.com/syumai/workers"
2024-01-03 20:09:47 +09:00
"github.com/syumai/workers/cloudflare/sockets"
2023-06-26 03:16:43 -05:00
)
func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2024-01-03 21:22:52 +09:00
conn, err := sockets.Connect(req.Context(), "tcpbin.com:4242", nil)
2023-06-26 03:16:43 -05:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(1 * time.Hour))
2023-06-26 03:22:29 -05:00
_, err = conn.Write([]byte("hello.\n"))
2023-06-26 03:16:43 -05:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rd := bufio.NewReader(conn)
bts, err := rd.ReadBytes('.')
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(bts)
})
workers.Serve(handler)
}