Skip to content

Commit

Permalink
final changes before API freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
igm committed May 20, 2014
1 parent 92f894b commit e3f2a6b
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 32 deletions.
8 changes: 4 additions & 4 deletions examples/webchat/webchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ func main() {
log.Fatal(http.ListenAndServe(":8080", nil))
}

func echoHandler(conn sockjs.Conn) {
func echoHandler(conn sockjs.Session) {
log.Println("new sockjs connection established")
var closedConn = make(chan struct{})
var closedSession = make(chan struct{})
chat.Publish("[info] new participant joined chat")
defer chat.Publish("[info] participant left chat")
go func() {
reader, _ := chat.SubChannel(nil)
for {
select {
case <-closedConn:
case <-closedSession:
return
case msg := <-reader:
if err := conn.Send(msg.(string)); err != nil {
Expand All @@ -43,6 +43,6 @@ func echoHandler(conn sockjs.Conn) {
}
break
}
close(closedConn)
close(closedSession)
log.Println("sockjs connection closed")
}
2 changes: 1 addition & 1 deletion examples/webecho/webecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
log.Fatal(http.ListenAndServe(":8080", nil))
}

func echoHandler(conn sockjs.Conn) {
func echoHandler(conn sockjs.Session) {
log.Println("new sockjs connection established")
for {
if msg, err := conn.Recv(); err == nil {
Expand Down
4 changes: 4 additions & 0 deletions sockjs/doc.go
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/*
Package sockjs is a server side implementation of sockjs protocol.
*/

package sockjs
4 changes: 2 additions & 2 deletions sockjs/example_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func ExampleNewHandler_simple() {
handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, func(con sockjs.Conn) {
handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, func(con sockjs.Session) {
var msg string
var err error
for {
Expand All @@ -23,7 +23,7 @@ func ExampleNewHandler_simple() {
}

func ExampleNewHandler_defaultMux() {
handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, func(con sockjs.Conn) {
handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, func(con sockjs.Session) {
var msg string
var err error
for {
Expand Down
4 changes: 2 additions & 2 deletions sockjs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type handler struct {
prefix string
options Options
handlerFunc func(Conn)
handlerFunc func(Session)
mappings []*mapping

sessionsMux sync.Mutex
Expand All @@ -21,7 +21,7 @@ type handler struct {

// NewHandler creates new HTTP handler that conforms to the basic net/http.Handler interface.
// It takes path prefix, options and sockjs handler function as parameters
func NewHandler(prefix string, opts Options, handlerFunc func(Conn)) *handler {
func NewHandler(prefix string, opts Options, handlerFunc func(Session)) *handler {
h := &handler{
prefix: prefix,
options: opts,
Expand Down
4 changes: 2 additions & 2 deletions sockjs/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func TestHandler_ParseSessionId(t *testing.T) {
func TestHandler_SessionByRequest(t *testing.T) {
h := NewHandler("", DefaultOptions, nil)
h.options.DisconnectDelay = 10 * time.Millisecond
var handlerFuncCalled = make(chan Conn)
h.handlerFunc = func(conn Conn) { handlerFuncCalled <- conn }
var handlerFuncCalled = make(chan Session)
h.handlerFunc = func(conn Session) { handlerFuncCalled <- conn }
req, _ := http.NewRequest("POST", "/server/sessionid/whatever/follows", nil)
sess, err := h.sessionByRequest(req)
if sess == nil || err != nil {
Expand Down
7 changes: 4 additions & 3 deletions sockjs/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
)

var (
// ErrSessionNotOpen error is returned in case of Session not open
ErrSessionNotOpen = errors.New("sockjs: session not in open state")
errSessionReceiverAttached = errors.New("sockjs: another receiver already attached")
)
Expand Down Expand Up @@ -66,10 +67,10 @@ type receiver interface {
}

// Session is a central component that handles receiving and sending frames. It maintains internal state
func newSession(sessionId string, sessionTimeoutInterval, heartbeatInterval time.Duration) *session {
func newSession(sessionID string, sessionTimeoutInterval, heartbeatInterval time.Duration) *session {
r, w := io.Pipe()
s := &session{
id: sessionId,
id: sessionID,
msgReader: r,
msgWriter: w,
msgEncoder: gob.NewEncoder(w),
Expand Down Expand Up @@ -211,4 +212,4 @@ func (s *session) Send(msg string) error {
return s.sendMessage(msg)
}

func (s *session) SessionId() string { return s.id }
func (s *session) ID() string { return s.id }
16 changes: 8 additions & 8 deletions sockjs/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ func TestSession_Closing(t *testing.T) {
}
}

// Session as Conn Tests
func TestSession_AsConn(t *testing.T) { var _ Conn = newSession("id", 0, 0) }
// Session as Session Tests
func TestSession_AsSession(t *testing.T) { var _ Session = newSession("id", 0, 0) }

func TestSession_ConnRecv(t *testing.T) {
func TestSession_SessionRecv(t *testing.T) {
s := newTestSession()
go func() {
s.accept("message 1")
Expand All @@ -235,7 +235,7 @@ func TestSession_ConnRecv(t *testing.T) {
}
}

func TestSession_ConnSend(t *testing.T) {
func TestSession_SessionSend(t *testing.T) {
s := newTestSession()
err := s.Send("message A")
if err != nil {
Expand All @@ -246,7 +246,7 @@ func TestSession_ConnSend(t *testing.T) {
}
}

func TestSession_ConnClose(t *testing.T) {
func TestSession_SessionClose(t *testing.T) {
s := newTestSession()
s.state = sessionActive
recv := newTestReceiver()
Expand Down Expand Up @@ -284,10 +284,10 @@ func TestSession_ConnClose(t *testing.T) {
}
}

func TestSession_ConnSessionId(t *testing.T) {
func TestSession_SessionSessionId(t *testing.T) {
s := newTestSession()
if s.SessionId() != "sessionId" {
t.Errorf("Unexpected session Id, got '%s', expected '%s'", s.SessionId(), "sessionId")
if s.ID() != "sessionId" {
t.Errorf("Unexpected session ID, got '%s', expected '%s'", s.ID(), "sessionId")
}
}

Expand Down
10 changes: 8 additions & 2 deletions sockjs/sockjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package sockjs

import "net/http"

// Handler is a interface that is returned by NewHandler() method that.
type Handler interface {
http.Handler
Prefix() string
}

type Conn interface {
// Session represents a connection between server and client. This is 1 to 1 relation.
type Session interface {
// Id returns a session id
ID() string
// Recv reads one text frame from session
Recv() (string, error)
// Send sends one text frame to session
Send(string) error
// Close closes the session with provided code and reason.
Close(status uint32, reason string) error
SessionId() string
}
10 changes: 5 additions & 5 deletions sockjs/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestHandler_WebSocket(t *testing.T) {
t.Errorf("Connection should be nil, got '%v'", conn)
}
// another request with "origin" set properly
var connCh = make(chan Conn)
h.handlerFunc = func(conn Conn) { connCh <- conn }
var connCh = make(chan Session)
h.handlerFunc = func(conn Session) { connCh <- conn }
conn, resp, err = websocket.DefaultDialer.Dial(url, map[string][]string{"Origin": []string{server.URL}})
if err != nil {
t.Errorf("Unexpected error '%v'", err)
Expand All @@ -59,7 +59,7 @@ func TestHandler_WebSocketTerminationByServer(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(h.sockjsWebsocket))
defer server.Close()
url := "ws" + server.URL[4:]
h.handlerFunc = func(conn Conn) {
h.handlerFunc = func(conn Session) {
conn.Close(1024, "some close message")
conn.Close(0, "this should be ognored")
}
Expand All @@ -84,7 +84,7 @@ func TestHandler_WebSocketTerminationByClient(t *testing.T) {
defer server.Close()
url := "ws" + server.URL[4:]
var done = make(chan struct{})
h.handlerFunc = func(conn Conn) {
h.handlerFunc = func(conn Session) {
if _, err := conn.Recv(); err != ErrSessionNotOpen {
t.Errorf("Recv should fail")
}
Expand All @@ -101,7 +101,7 @@ func TestHandler_WebSocketCommunication(t *testing.T) {
// defer server.CloseClientConnections()
url := "ws" + server.URL[4:]
var done = make(chan struct{})
h.handlerFunc = func(conn Conn) {
h.handlerFunc = func(conn Session) {
conn.Send("message 1")
conn.Send("message 2")
msg, err := conn.Recv()
Expand Down
6 changes: 3 additions & 3 deletions testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (t testHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
http.NotFound(rw, req)
}

func echoHandler(conn sockjs.Conn) {
func echoHandler(conn sockjs.Session) {
log.Println("New connection created")
for {
if msg, err := conn.Recv(); err != nil {
Expand All @@ -65,9 +65,9 @@ func echoHandler(conn sockjs.Conn) {
}
}
}
log.Println("Connection closed")
log.Println("Sessionection closed")
}

func closeHandler(conn sockjs.Conn) {
func closeHandler(conn sockjs.Session) {
conn.Close(3000, "Go away!")
}

0 comments on commit e3f2a6b

Please sign in to comment.