|
|
|
package websocket
|
|
|
|
|
|
|
|
import "go.uber.org/zap/zapcore"
|
|
|
|
|
|
|
|
// StatusCode is the code used by the WebSocket protocol to signal the other side on close.
|
|
|
|
type StatusCode int16
|
|
|
|
|
|
|
|
const (
|
|
|
|
StatusNormal StatusCode = 1000
|
|
|
|
StatusGoingAway StatusCode = 1001
|
|
|
|
StatusProtocolError StatusCode = 1002
|
|
|
|
StatusTooBig StatusCode = 1009
|
|
|
|
StatusProtocolVersionOutOfDate StatusCode = 4000
|
|
|
|
|
|
|
|
GoodbyeType = "GOODBYE"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SocketClosed is synthesized when a client closes the WebSocket connection, or sent to the write process to write a
|
|
|
|
// WebSocket close message.
|
|
|
|
// Sending a SocketClosed on a channel causes that channel to be closed right after.
|
|
|
|
type SocketClosed struct {
|
|
|
|
// Code is the StatusCode given (or which should be given) in the close message.
|
|
|
|
Code StatusCode `json:"code"`
|
|
|
|
// Text is the reason text given (or which should be given) in the close message. Max 123 characters.
|
|
|
|
Text string `json:"text"`
|
|
|
|
// Error may be an error that resulted in the closure of the socket.
|
|
|
|
// Will not be written by the writer; only useful when it's returned from the reader.
|
|
|
|
Error error `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c SocketClosed) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
|
|
encoder.AddString("type", GoodbyeType)
|
|
|
|
encoder.AddInt16("code", int16(c.Code))
|
|
|
|
encoder.AddString("text", c.Text)
|
|
|
|
if c.Error != nil {
|
|
|
|
encoder.AddString("error", c.Error.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c SocketClosed) ClientType() ClientMessageType {
|
|
|
|
return ClientGoodbyeType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c SocketClosed) ServerType() ServerMessageType {
|
|
|
|
return ServerGoodbyeType
|
|
|
|
}
|