Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Correctly send close message when closing websocket connection
Browse files Browse the repository at this point in the history
Signed-off-by: Maya Rosecrance <mrosecrance@pivotal.io>
  • Loading branch information
John Tuley committed Jan 30, 2015
1 parent 702fc9d commit 748c158
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/handlers/websocket_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (h *websocketHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
return
}
defer ws.Close()
defer ws.WriteControl(websocket.CloseMessage, []byte{}, time.Time{})
defer ws.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Time{})
keepAliveExpired := make(chan struct{})

// TODO: remove this loop (but keep ws.ReadMessage()) once we retire support in the cli for old style keep alives
Expand Down
14 changes: 12 additions & 2 deletions server/handlers/websocket_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"github.com/cloudfoundry/loggregatorlib/loggertesthelper"
"github.com/cloudfoundry/loggregatorlib/server/handlers"
"github.com/gorilla/websocket"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io"
"net/http"
"net/http/httptest"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("WebsocketHandler", func() {
Expand Down Expand Up @@ -118,6 +120,14 @@ var _ = Describe("WebsocketHandler", func() {
close(messagesChan)
})

It("should send a closing message", func() {
ws, _, err := websocket.DefaultDialer.Dial(httpToWs(testServer.URL), nil)
Expect(err).NotTo(HaveOccurred())
close(messagesChan)
_, _, err = ws.ReadMessage()
Expect(err).To(Equal(io.EOF))
})

})

func httpToWs(u string) string {
Expand Down

0 comments on commit 748c158

Please sign in to comment.