You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
3 years ago
|
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"
|
||
|
)
|
||
|
|
||
|
// TODO: Noting that there should be three channels in play:
|
||
|
// 1) Reader to client: to receive messages from the connection
|
||
|
// 2) Client to writer: to send messages on the connection
|
||
|
// 3) Writer to reader: indicating that it is about to send a close message, and the reader should wait for one and
|
||
|
// time out the connection if it takes too long.
|
||
|
|
||
|
// 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
|
||
|
// Text is the reason text given (or which should be given) in the close message. Max 123 characters.
|
||
|
Text string
|
||
|
// 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
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|