dummy cli
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
@@ -64,20 +65,28 @@ type S struct {
|
||||
|
||||
func (s *S) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if err := s.serveHTTP(w, r); err != nil {
|
||||
log.Println(r.URL.Path, "//", err.Error(), r.Header)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *S) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||
switch strings.Split(r.URL.Path[1:], "/")[0] {
|
||||
case "ws", "v1":
|
||||
if isV1(r) || isWS(r) {
|
||||
return s.serveAPI(w, r)
|
||||
}
|
||||
return s.serveStatic(w, r)
|
||||
}
|
||||
|
||||
func isV1(r *http.Request) bool {
|
||||
return strings.HasPrefix(r.URL.Path, "/v1/")
|
||||
}
|
||||
|
||||
func isWS(r *http.Request) bool {
|
||||
return r.URL.Path == "/ws"
|
||||
}
|
||||
|
||||
func (s *S) serveStatic(w http.ResponseWriter, r *http.Request) error {
|
||||
return io.EOF
|
||||
return fmt.Errorf("not impl static")
|
||||
}
|
||||
|
||||
func (s *S) serveAPI(w http.ResponseWriter, r *http.Request) error {
|
||||
@@ -88,12 +97,12 @@ func (s *S) serveAPI(w http.ResponseWriter, r *http.Request) error {
|
||||
return err
|
||||
}
|
||||
|
||||
switch strings.Split(r.URL.Path[1:], "/")[0] {
|
||||
case "ws":
|
||||
if isWS(r) {
|
||||
return s.serveWS(w, r)
|
||||
case "v1":
|
||||
} else if isV1(r) {
|
||||
return s.serveV1(w, r)
|
||||
}
|
||||
|
||||
http.NotFound(w, r)
|
||||
return nil
|
||||
}
|
||||
@@ -115,10 +124,22 @@ func (s *S) injectContext(w http.ResponseWriter, r *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S) serveWS(w http.ResponseWriter, r *http.Request) error {
|
||||
return io.EOF
|
||||
func (s *S) serveWS(httpw http.ResponseWriter, httpr *http.Request) error {
|
||||
ctx := httpr.Context()
|
||||
|
||||
c, err := websocket.Accept(httpw, httpr, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.CloseNow()
|
||||
|
||||
if err := c.Write(ctx, 1, []byte("hello world")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
|
||||
return io.EOF
|
||||
return fmt.Errorf("not impl: v1")
|
||||
}
|
||||
|
||||
34
cmd/testws/main.go
Normal file
34
cmd/testws/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||
defer can()
|
||||
|
||||
c, _, err := websocket.Dial(ctx, os.Args[1], &websocket.DialOptions{
|
||||
HTTPHeader: map[string][]string{
|
||||
"Cookie": []string{"uuid=x"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer c.CloseNow()
|
||||
|
||||
for {
|
||||
mt, b, err := c.Read(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Printf("[%s] %s", mt, b)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user