Skip to content

Commit

Permalink
release
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Jan 2, 2023
1 parent d223cfb commit 1cac093
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 280 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# gws
### minimal websocket server

#### Highlight
- No dependency
- Fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite)

#### Attention
- it's designed for api server, do not write big message
- it's recommended not to enable data compression in the intranet
- need to manage your own message handling coroutine
- It's designed for api server, do not write big message
- It's recommended not to enable data compression in the intranet
- You need to manage your own message handling coroutine

#### Quick Start
```go
Expand Down
88 changes: 0 additions & 88 deletions example/tests/handler.go

This file was deleted.

91 changes: 0 additions & 91 deletions example/tests/index.html

This file was deleted.

78 changes: 0 additions & 78 deletions example/tests/server.go

This file was deleted.

2 changes: 1 addition & 1 deletion example/testsuite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func main() {
var upgrader = gws.Upgrader{}
var upgrader = gws.Upgrader{CompressEnabled: true, MaxContentLength: 32 * 1024 * 1024}
var handler = new(WebSocketHandler)
ctx, cancel := context.WithCancel(context.Background())

Expand Down
9 changes: 0 additions & 9 deletions init.go

This file was deleted.

9 changes: 5 additions & 4 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

type Message struct {
err error
opcode Opcode
dbuf *internal.Buffer // 数据缓冲
cbuf *internal.Buffer // 解码器缓冲
err error
opcode Opcode
compressed bool
dbuf *internal.Buffer // 数据缓冲
cbuf *internal.Buffer // 解码器缓冲
}

func (c *Message) Read(p []byte) (n int, err error) {
Expand Down
1 change: 1 addition & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type EventHandler interface {
}

var closeErrorMap = map[CloseCode]string{
0: "empty code",
CloseNormalClosure: "close normal",
CloseGoingAway: "client going away",
CloseProtocolError: "protocol error",
Expand Down
13 changes: 10 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"unicode/utf8"
)

var _pool *internal.BufferPool

func init() {
_pool = internal.NewBufferPool()
}

func (c *Conn) ReadMessage() <-chan *Message {
return c.messageChan
}
Expand Down Expand Up @@ -93,7 +99,7 @@ func (c *Conn) readMessage() error {
// the negotiated extensions defines the meaning of such a nonzero
// value, the receiving endpoint MUST _Fail the WebSocket
// Connection_.
if c.fh.GetRSV1() != c.compressEnabled || c.fh.GetRSV2() || c.fh.GetRSV3() {
if !c.compressEnabled && (c.fh.GetRSV1() || c.fh.GetRSV2() || c.fh.GetRSV3()) {
return CloseProtocolError
}

Expand All @@ -103,6 +109,7 @@ func (c *Conn) readMessage() error {

// read control frame
var opcode = c.fh.GetOpcode()
var compressed = c.compressEnabled && c.fh.GetRSV1()
if !isDataFrame(opcode) {
return c.readControl()
}
Expand Down Expand Up @@ -161,7 +168,7 @@ func (c *Conn) readMessage() error {
return CloseMessageTooLarge
}
if fin {
msg := &Message{opcode: c.continuationOpcode, dbuf: c.continuationBuffer}
msg := &Message{opcode: c.continuationOpcode, dbuf: c.continuationBuffer, compressed: compressed}
c.continuationOpcode = 0
c.continuationBuffer = nil
return c.emitMessage(msg)
Expand All @@ -176,7 +183,7 @@ func (c *Conn) readMessage() error {

switch opcode {
case OpcodeText, OpcodeBinary:
return c.emitMessage(&Message{opcode: opcode, dbuf: buf})
return c.emitMessage(&Message{opcode: opcode, dbuf: buf, compressed: compressed})
default:
return errors.New("unexpected opcode: " + strconv.Itoa(int(opcode)))
}
Expand Down
Loading

0 comments on commit 1cac093

Please sign in to comment.