Skip to content

Commit

Permalink
add getter for session transport protocol
Browse files Browse the repository at this point in the history
This is useful when diagnosing issues with SockJS that may be related to what
kind of protocol a connection is using.
  • Loading branch information
Morfent committed Dec 20, 2016
1 parent 1f275fb commit 5748021
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sockjs/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"net/http"
"path"
"sync"
"time"
)
Expand Down Expand Up @@ -222,6 +223,11 @@ func (s *session) Send(msg string) error {

func (s *session) ID() string { return s.id }

func (s *session) Protocol() string {
protocol := path.Base(s.req.URL.Path)
return protocol
}

func (s *session) GetSessionState() SessionState {
s.RLock()
defer s.RUnlock()
Expand Down
13 changes: 13 additions & 0 deletions sockjs/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sockjs
import (
"io"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -303,6 +304,18 @@ func TestSession_SessionSessionId(t *testing.T) {
}
}

func TestSession_SessionProtocol(t *testing.T) {
h := newTestHandler()
server := httptest.NewServer(http.HandlerFunc(h.sockjsWebsocket))
defer server.Close()
req, _ := http.NewRequest("GET", server.URL, nil)
req.URL.Path += "websocket"
s := newSession(req, "sessionID", 1000*time.Second, 1000*time.Second)
if s.Protocol() != "websocket" {
t.Errorf("Unexpected transport protocol, got '%s', expected '%s'", s.Protocol(), "websocket")
}
}

func newTestReceiver() *testReceiver {
return &testReceiver{
doneCh: make(chan struct{}),
Expand Down
2 changes: 2 additions & 0 deletions sockjs/sockjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import "net/http"
type Session interface {
// Id returns a session id
ID() string
// Protocol used for the transport
Protocol() string
// Request returns the first http request
Request() *http.Request
// Recv reads one text frame from session
Expand Down

0 comments on commit 5748021

Please sign in to comment.