Allow sending custom close messages

This commit is contained in:
Robbie Trencheny
2017-03-28 13:48:20 -07:00
parent e5f177a9d7
commit c773bc2f72
3 changed files with 57 additions and 6 deletions
+14 -1
View File
@@ -2,10 +2,11 @@ package melody
import (
"errors"
"github.com/gorilla/websocket"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
// Session wrapper around websocket connections.
@@ -165,6 +166,18 @@ func (s *Session) Close() error {
return nil
}
// CloseWithMsg closes the session with the provided payload.
// Use the FormatCloseMessage function to format a proper close message payload.
func (s *Session) CloseWithMsg(msg []byte) error {
if s.closed() {
return errors.New("Session is already closed.")
}
s.writeMessage(&envelope{t: websocket.CloseMessage, msg: msg})
return nil
}
// Set is used to store a new key/value pair exclusivelly for this session.
// It also lazy initializes s.Keys if it was not used previously.
func (s *Session) Set(key string, value interface{}) {