Skip to content

Commit

Permalink
Merge pull request #10 from tarndt/ta-addHandleHelper
Browse files Browse the repository at this point in the history
Make using wsl as http.Handler easier.
  • Loading branch information
tarndt authored Dec 30, 2019
2 parents e623f18 + ad8a682 commit 0f186a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
//Setup HTTP / Websocket server
router := http.NewServeMux()
wsl := wasmws.NewWebSocketListener(appCtx)
router.HandleFunc("/grpc-proxy", wsl.HTTPAccept)
router.HandleFunc("/grpc-proxy", wsl.ServeHTTP)
router.Handle("/", http.FileServer(http.Dir("./static")))
httpServer := &http.Server{Addr: ":8080", Handler: router}
//Run HTTP server
Expand Down
9 changes: 6 additions & 3 deletions wslistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ type WebSockListener struct {
acceptCh chan net.Conn
}

var _ net.Listener = (*WebSockListener)(nil)
var (
_ net.Listener = (*WebSockListener)(nil)
_ http.Handler = (*WebSockListener)(nil)
)

//NewWebSocketListener constructs a new WebSockListener, the provided context
//is for the lifetime of the listener.
Expand All @@ -47,9 +50,9 @@ func NewWebSocketListener(ctx context.Context) *WebSockListener {
return wsl
}

//HTTPAccept is a method that is mean to be used as http.HandlerFunc to accept inbound HTTP requests
//ServeHTTP is a method that is mean to be used as http.HandlerFunc to accept inbound HTTP requests
// that are websocket connections
func (wsl *WebSockListener) HTTPAccept(wtr http.ResponseWriter, req *http.Request) {
func (wsl *WebSockListener) ServeHTTP(wtr http.ResponseWriter, req *http.Request) {
select {
case <-wsl.ctx.Done():
http.Error(wtr, "503: Service is shutdown", http.StatusServiceUnavailable)
Expand Down

0 comments on commit 0f186a8

Please sign in to comment.