Documentation ¶
Index ¶
- Constants
- func BytesToString(bytes []byte) string
- func CloseReader(r *MessageReader) error
- func CloseWriter(writer *MessageWriter) error
- func HeaderValueCheck(header http.Header, key string, val string) bool
- func MarshalCloseInfo(code CloseCode, text string) ([]byte, error)
- func OriginHostCheck(r *http.Request) bool
- func PutByteBuffer(b *ByteBuffer)
- func SetDebugLogger(logger DebugLogger)
- func SetFrameReadBuffSize(size int)
- func SetLoggerPrefix(prefix string)
- func SetMessageBufferSize(size int)
- func SetMinCompressSize(size int)
- func SetOriginCheckFunc(fn OriginCheckFunc)
- func ShowDebugLog(show bool)
- func StringToBytes(s string) []byte
- func UseFlate(val ...CompressAlloter)
- func WriteAllTo(buff []byte, dst io.Writer) error
- type AcceptOptions
- type ByteBuffer
- func (b *ByteBuffer) CloneBytes() []byte
- func (b *ByteBuffer) Close()
- func (b *ByteBuffer) GetBytes() []byte
- func (b *ByteBuffer) GetString() string
- func (b *ByteBuffer) GetVolatileString() string
- func (b *ByteBuffer) ReadAll(reader io.Reader) error
- func (b *ByteBuffer) ReadFull(reader io.Reader) error
- func (b *ByteBuffer) Reset()
- func (b *ByteBuffer) Size() int
- type ChuckReadHandler
- type CloseCode
- type CloseInfo
- type CompressAlloter
- type CompressMode
- type DebugLogger
- type ExtParam
- type FlateAlloter
- func (this *FlateAlloter) FlushWriter(fw io.WriteCloser) error
- func (this *FlateAlloter) NewReader(mr *FrameReader) (io.ReadCloser, error)
- func (this *FlateAlloter) NewWriter(mw *FrameWriter) (io.WriteCloser, error)
- func (this *FlateAlloter) ResetReader(fr io.ReadCloser, mr *FrameReader) error
- func (this *FlateAlloter) ResetWriter(fw io.WriteCloser, mw *FrameWriter) error
- func (this *FlateAlloter) WebsocketExtension(params []string) string
- type FrameHeader
- type FrameReader
- type FrameWriter
- func (w *FrameWriter) Close() error
- func (w *FrameWriter) GetPayload() []byte
- func (w *FrameWriter) GetPayloadLength() int
- func (w *FrameWriter) Reset(ws *WebSocket, opcode int) error
- func (w *FrameWriter) SetTrimlength(trim int)
- func (w *FrameWriter) Write(p []byte) (int, error)
- func (w *FrameWriter) WriteAll(data []byte) error
- func (w *FrameWriter) WriteControlFrame(data []byte) error
- func (w *FrameWriter) WriteString(str string) error
- type MessageHandler
- type MessageReader
- type MessageWriter
- type OriginCheckFunc
- type SimpleLogger
- type SocketHandler
- type TokenInfo
- type WebSocket
- func (ws *WebSocket) Close() error
- func (ws *WebSocket) CloseHandshake(code CloseCode, text string) error
- func (ws *WebSocket) LocalAddr() net.Addr
- func (ws *WebSocket) NextReader() (FrameHeader, *MessageReader, error)
- func (ws *WebSocket) NextWriter(opcode int) *MessageWriter
- func (ws *WebSocket) RemoteAddr() net.Addr
- func (ws *WebSocket) ServeRead(handler MessageHandler)
- func (ws *WebSocket) SetReadTimeOut(readTimeOut time.Duration)
- func (ws *WebSocket) SetWriteTimeOut(writeTimeOut time.Duration)
- func (ws *WebSocket) WriteBinary(data []byte) error
- func (ws *WebSocket) WriteJSON(v interface{}) error
- func (ws *WebSocket) WriteMessage(opcode int, data []byte) error
- func (ws *WebSocket) WritePing(data []byte) error
- func (ws *WebSocket) WritePong(data []byte) error
- func (ws *WebSocket) WriteString(data string) error
- func (ws *WebSocket) WriteText(data []byte) error
Constants ¶
View Source
const ( Frame_Null = -1 Frame_Continue = 0 Frame_Text = 1 Frame_Binary = 2 Frame_Close = 8 Frame_Ping = 9 Frame_Pong = 10 )
Variables ¶
This section is empty.
Functions ¶
func BytesToString ¶
func CloseReader ¶
func CloseReader(r *MessageReader) error
func CloseWriter ¶
func CloseWriter(writer *MessageWriter) error
关闭MessageWriter,MessageWriter关闭后会解除对NextWriter调用的阻塞
func OriginHostCheck ¶
func PutByteBuffer ¶
func PutByteBuffer(b *ByteBuffer)
func SetDebugLogger ¶
func SetDebugLogger(logger DebugLogger)
func SetFrameReadBuffSize ¶
func SetFrameReadBuffSize(size int)
func SetLoggerPrefix ¶
func SetLoggerPrefix(prefix string)
func SetMessageBufferSize ¶
func SetMessageBufferSize(size int)
func SetMinCompressSize ¶
func SetMinCompressSize(size int)
func SetOriginCheckFunc ¶ added in v1.1.7
func SetOriginCheckFunc(fn OriginCheckFunc)
func ShowDebugLog ¶
func ShowDebugLog(show bool)
func StringToBytes ¶
func UseFlate ¶
func UseFlate(val ...CompressAlloter)
Types ¶
type AcceptOptions ¶
type AcceptOptions struct { SubProtocols []string // contains filtered or unexported fields }
func DefaultAcceptOptions ¶
func DefaultAcceptOptions() *AcceptOptions
func NewAcceptOptions ¶
func NewAcceptOptions() *AcceptOptions
func (*AcceptOptions) SetFrameReadBuffSize ¶
func (this *AcceptOptions) SetFrameReadBuffSize(size int)
func (*AcceptOptions) SetMessageBufferSize ¶
func (this *AcceptOptions) SetMessageBufferSize(size int)
func (*AcceptOptions) SetMinCompressSize ¶
func (this *AcceptOptions) SetMinCompressSize(size int)
func (*AcceptOptions) SetOriginCheckFunc ¶ added in v1.1.7
func (this *AcceptOptions) SetOriginCheckFunc(fn OriginCheckFunc)
func (*AcceptOptions) UseFlate ¶
func (this *AcceptOptions) UseFlate(val ...CompressAlloter)
type ByteBuffer ¶
type ByteBuffer struct {
// contains filtered or unexported fields
}
func GetByteBuffer ¶
func GetByteBuffer(opts *AcceptOptions) *ByteBuffer
func (*ByteBuffer) CloneBytes ¶
func (b *ByteBuffer) CloneBytes() []byte
func (*ByteBuffer) Close ¶
func (b *ByteBuffer) Close()
func (*ByteBuffer) GetBytes ¶
func (b *ByteBuffer) GetBytes() []byte
func (*ByteBuffer) GetString ¶
func (b *ByteBuffer) GetString() string
func (*ByteBuffer) GetVolatileString ¶
func (b *ByteBuffer) GetVolatileString() string
func (*ByteBuffer) Reset ¶
func (b *ByteBuffer) Reset()
func (*ByteBuffer) Size ¶
func (b *ByteBuffer) Size() int
type ChuckReadHandler ¶ added in v1.1.7
type ChuckReadHandler interface { SocketHandler OnRead(ws *WebSocket, opcode int, fin bool, buff *ByteBuffer) error }
type CloseCode ¶
type CloseCode uint16
const ( //正常关闭; 无论为何目的而创建, 该链接都已成功完成任务. CloseNormalClosure CloseCode = 1000 //终端离开:可能因为服务端错误, 也可能因为浏览器正从打开连接的页面跳转离开. CloseGoingAway CloseCode = 1001 //协议错误:由于协议错误而中断连接. CloseProtocolError CloseCode = 1002 //数据格式错误:由于接收到不允许的数据类型而断开连接 CloseUnsupportedData CloseCode = 1003 //保留 CloseReserved CloseCode = 1004 //没有收到预期的状态码. CloseNoCloseRcvd CloseCode = 1005 //异常关闭:用于期望收到状态码时连接非正常关闭 (也就是说, 没有发送关闭帧). CloseAbnormalClosure CloseCode = 1006 //由于收到了格式不符的数据而断开连接 (如文本消息中包含了非 UTF-8 数据). CloseInvalidPayload CloseCode = 1007 //由于收到不符合约定的数据而断开连接. 这是一个通用状态码, 用于不适合使用 1003 和 1009 状态码的场景. ClosePolicyViolation CloseCode = 1008 //由于收到过大的数据帧而断开连接. CloseMessageTooBig CloseCode = 1009 //缺少扩展:客户端终止连接,因为期望一个或多个拓展, 但服务器没有. CloseMandatoryExtension CloseCode = 1010 //内部错误:服务器终止连接,因为遇到异常 CloseInternalError CloseCode = 1011 //服务重启:服务器由于重启而断开连接. CloseServiceRestart CloseCode = 1012 //稍后再试:服务器由于临时原因断开连接。 CloseTryAgainLater CloseCode = 1013 //错误的网关. CloseBadGateway CloseCode = 1014 //握手错误:表示连接由于无法完成 TLS 握手而关闭 (例如无法验证服务器证书). CloseTLSHandshake CloseCode = 1015 )
type CompressAlloter ¶
type CompressAlloter interface { WebsocketExtension(args []string) string NewWriter(mw *FrameWriter) (io.WriteCloser, error) FlushWriter(fw io.WriteCloser) error ResetWriter(fw io.WriteCloser, mw *FrameWriter) error NewReader(mr *FrameReader) (io.ReadCloser, error) ResetReader(fr io.ReadCloser, mr *FrameReader) error }
type CompressMode ¶
type CompressMode int
const ( CompressDisabled CompressMode = iota CompressContextTakeover CompressNoContextTakeover )
type DebugLogger ¶
type FlateAlloter ¶
type FlateAlloter struct {
// contains filtered or unexported fields
}
func NewFlateAlloter ¶
func NewFlateAlloter(level int) *FlateAlloter
func (*FlateAlloter) FlushWriter ¶ added in v1.1.7
func (this *FlateAlloter) FlushWriter(fw io.WriteCloser) error
func (*FlateAlloter) NewReader ¶
func (this *FlateAlloter) NewReader(mr *FrameReader) (io.ReadCloser, error)
func (*FlateAlloter) NewWriter ¶
func (this *FlateAlloter) NewWriter(mw *FrameWriter) (io.WriteCloser, error)
func (*FlateAlloter) ResetReader ¶
func (this *FlateAlloter) ResetReader(fr io.ReadCloser, mr *FrameReader) error
func (*FlateAlloter) ResetWriter ¶
func (this *FlateAlloter) ResetWriter(fw io.WriteCloser, mw *FrameWriter) error
func (*FlateAlloter) WebsocketExtension ¶ added in v1.1.7
func (this *FlateAlloter) WebsocketExtension(params []string) string
type FrameHeader ¶
type FrameHeader struct {
// contains filtered or unexported fields
}
type FrameReader ¶
type FrameReader struct {
// contains filtered or unexported fields
}
func (*FrameReader) BufferSize ¶
func (mr *FrameReader) BufferSize() int
func (*FrameReader) Close ¶
func (mr *FrameReader) Close() error
func (*FrameReader) Read ¶
func (mr *FrameReader) Read(p []byte) (int, error)
mr.extra是结束标志:"\x01\x00\x00\xff\xff"来防止flate.reader产生:unexpected EOF错误 首先从网络接口读取消息数据,消息数据读取完成后从mr.extra读取压缩数据结束标志
func (*FrameReader) ReadMessagePayload ¶
func (mr *FrameReader) ReadMessagePayload(p []byte) (int, error)
type FrameWriter ¶
type FrameWriter struct {
// contains filtered or unexported fields
}
func NewFrameWriter ¶
func NewFrameWriter(ws *WebSocket, opcode int) *FrameWriter
func (*FrameWriter) Close ¶
func (w *FrameWriter) Close() error
init后,opcode != (Frame_Continue, Frame_Null) 写数据后,opcode == (Frame_Continue, Frame_Null) colse后,opcode == Frame_Null
colse前检查是否已经发送了结束帧(opcode == Frame_Null), 若已经发送了结束帧,则退出
func (*FrameWriter) GetPayload ¶
func (w *FrameWriter) GetPayload() []byte
func (*FrameWriter) GetPayloadLength ¶
func (w *FrameWriter) GetPayloadLength() int
func (*FrameWriter) SetTrimlength ¶
func (w *FrameWriter) SetTrimlength(trim int)
func (*FrameWriter) WriteAll ¶
func (w *FrameWriter) WriteAll(data []byte) error
func (*FrameWriter) WriteControlFrame ¶
func (w *FrameWriter) WriteControlFrame(data []byte) error
func (*FrameWriter) WriteString ¶
func (w *FrameWriter) WriteString(str string) error
type MessageHandler ¶
type MessageHandler interface { SocketHandler OnMessage(ws *WebSocket, opcode int, buff *ByteBuffer) error }
type MessageReader ¶
type MessageReader struct {
// contains filtered or unexported fields
}
func (*MessageReader) Close ¶
func (mr *MessageReader) Close() error
func (*MessageReader) ReadAll ¶
func (mr *MessageReader) ReadAll() (*ByteBuffer, error)
type MessageWriter ¶
type MessageWriter struct {
// contains filtered or unexported fields
}
func (*MessageWriter) Close ¶
func (w *MessageWriter) Close() error
func (*MessageWriter) WriteAll ¶
func (w *MessageWriter) WriteAll(data []byte) error
func (*MessageWriter) WriteControlFrame ¶
func (w *MessageWriter) WriteControlFrame(data []byte) error
func (*MessageWriter) WriteString ¶
func (w *MessageWriter) WriteString(str string) error
type OriginCheckFunc ¶ added in v1.1.7
type SimpleLogger ¶
type SimpleLogger struct {
// contains filtered or unexported fields
}
func NewSimpleLogger ¶
func NewSimpleLogger() *SimpleLogger
func (*SimpleLogger) Output ¶
func (lg *SimpleLogger) Output(prefix string, msg string)
type SocketHandler ¶
type SocketHandler interface {
OnClose(ws *WebSocket)
}
type WebSocket ¶
type WebSocket struct {
// contains filtered or unexported fields
}
func Accept ¶
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions, headers map[string]string) (*WebSocket, error)
func NewWebSocket ¶
func (*WebSocket) CloseHandshake ¶ added in v1.1.8
func (*WebSocket) NextReader ¶
func (ws *WebSocket) NextReader() (FrameHeader, *MessageReader, error)
func (*WebSocket) NextWriter ¶
func (ws *WebSocket) NextWriter(opcode int) *MessageWriter
获取一个MessageWriter, 若存在已经创建的MessageWriter,则该调用被阻塞
func (*WebSocket) RemoteAddr ¶
func (*WebSocket) ServeRead ¶ added in v1.2.0
func (ws *WebSocket) ServeRead(handler MessageHandler)
func (*WebSocket) SetReadTimeOut ¶ added in v1.1.8
func (*WebSocket) SetWriteTimeOut ¶
func (*WebSocket) WriteBinary ¶
func (*WebSocket) WritePing ¶
Ping和Pong是websocket里的心跳,用来保证客户端是在线的, 目前浏览器中没有相关api发送ping给服务器,只能由服务器发ping给浏览器,浏览器返回pong消息。
func (*WebSocket) WriteString ¶ added in v1.2.0
Source Files ¶
- byte_buffer.go
- compress.go
- compress_flate.go
- func_header.go
- func_mask.go
- func_string.go
- logger.go
- websocket.go
- ws_accept.go
- ws_ccode.go
- ws_opcode.go
- ws_options.go
- ws_options_default.go
- ws_reader.go
- ws_reader_frame.go
- ws_reader_message.go
- ws_reader_pump.go
- ws_writer.go
- ws_writer_frame.go
- ws_writer_message.go
Click to show internal directories.
Click to hide internal directories.