Skip to content

Commit

Permalink
update document
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Jan 6, 2023
1 parent dce24f8 commit 8c22307
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ test:

bench:
go test -benchmem -bench ^Benchmark github.com/lxzan/gws

build:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/gws-server-linux-amd64 github.com/lxzan/gws/examples/testsuite
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#### Highlight
- websocket event api
- write in batch and flush
- no dependency
- zero goroutine to control websocket
- zero dependency
- zero extra goroutine to control websocket
- zero error to read/write message, errors have been handled appropriately
- fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite)

#### Attention
Expand All @@ -16,12 +16,12 @@
#### Interface
```go
type Event interface {
OnOpen(socket *Conn)
OnError(socket *Conn, err error)
OnClose(socket *Conn, message *Message)
OnMessage(socket *Conn, message *Message)
OnPing(socket *Conn, message *Message)
OnPong(socket *Conn, message *Message)
OnOpen(socket *Conn)
OnError(socket *Conn, err error)
OnClose(socket *Conn, code uint16, reason []byte)
OnPing(socket *Conn, payload []byte)
OnPong(socket *Conn, payload []byte)
OnMessage(socket *Conn, message *Message)
}
```

Expand All @@ -37,16 +37,14 @@ import (
)

func main() {
var upgrader = gws.Upgrader{CompressEnabled: true, MaxContentLength: 32 * 1024 * 1024}
var config = gws.Config{CompressEnabled: true, CheckTextEncoding: true, MaxContentLength: 32 * 1024 * 1024}
var handler = new(WebSocket)

http.HandleFunc("/connect", func(writer http.ResponseWriter, request *http.Request) {
socket, err := upgrader.Upgrade(context.Background(), writer, request, handler)
socket, err := gws.Accept(context.Background(), writer, request, handler, config)
if err != nil {
return
}

defer socket.Close()
socket.Listen()
})

Expand All @@ -55,9 +53,8 @@ func main() {

type WebSocket struct{}

func (c *WebSocket) OnClose(socket *gws.Conn, message *gws.Message) {
fmt.Printf("onclose: code=%d, payload=%s\n", message.Code(), string(message.Bytes()))
message.Close()
func (c *WebSocket) OnClose(socket *gws.Conn, code uint16, reason []byte) {
fmt.Printf("onclose: code=%d, payload=%s\n", code, string(reason))
}

func (c *WebSocket) OnError(socket *gws.Conn, err error) {
Expand All @@ -68,16 +65,15 @@ func (c *WebSocket) OnOpen(socket *gws.Conn) {
println("connected")
}

func (c *WebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {
socket.WriteMessage(message.Typ(), message.Bytes())
message.Close()
func (c *WebSocket) OnPing(socket *gws.Conn, payload []byte) {
fmt.Printf("onping: payload=%s\n", string(payload))
socket.WritePong(payload)
}

func (c *WebSocket) OnPing(socket *gws.Conn, message *gws.Message) {
fmt.Printf("onping: payload=%s\n", string(message.Bytes()))
socket.WritePong(message.Bytes())
func (c *WebSocket) OnPong(socket *gws.Conn, payload []byte) {}

func (c *WebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {
socket.WriteMessage(message.Typ(), message.Bytes())
message.Close()
}

func (c *WebSocket) OnPong(socket *gws.Conn, message *gws.Message) {}
```
2 changes: 1 addition & 1 deletion compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *decompressor) Decompress(payload *internal.Buffer) (*internal.Buffer, e
return nil, err
}

var buf = _pool.Get(payload.Len())
var buf = _pool.Get(3 * payload.Len())
_, err := io.Copy(buf, c.fr)
_pool.Put(payload)
return buf, err
Expand Down
3 changes: 0 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ func serveWebSocket(ctx context.Context, config Config, r *internal.Request, net
func (c *Conn) Listen() {
defer c.conn.Close()
for {
if atomic.LoadUint32(&c.closed) == 1 {
return
}
if err := c.readMessage(); err != nil {
c.emitError(err)
return
Expand Down
2 changes: 1 addition & 1 deletion examples/bench/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

ws.onmessage = function (msg) {
if (msg.data != "ok") {
// ws.send(msg.data);
ws.send(msg.data);
count++;
}
if (count == 1000000) {
Expand Down
3 changes: 2 additions & 1 deletion examples/bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
var ctx = context.Background()

http.HandleFunc("/connect", func(writer http.ResponseWriter, request *http.Request) {
socket, err := gws.Accept(ctx, writer, request, handler, gws.Config{CheckTextEncoding: true})
socket, err := gws.Accept(ctx, writer, request, handler, gws.Config{})
if err != nil {
return
}
Expand Down Expand Up @@ -104,6 +104,7 @@ func (c *WebSocket) OnTest(socket *gws.Conn) {
func (c *WebSocket) OnVerify(socket *gws.Conn) {
if socket.Len() != 0 {
socket.WriteMessage(gws.OpcodeText, []byte("failed"))
return
}
socket.WriteMessage(gws.OpcodeText, []byte("ok"))
}
Expand Down
4 changes: 4 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/lxzan/gws/internal"
"io"
"strconv"
"sync/atomic"
)

var _pool *internal.BufferPool
Expand Down Expand Up @@ -74,6 +75,9 @@ func (c *Conn) readControl() error {
}

func (c *Conn) readMessage() error {
if atomic.LoadUint32(&c.closed) == 1 {
return internal.CloseNormalClosure
}
if err := c.readN(c.fh[:2], 2); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gws
import (
"github.com/lxzan/gws/internal"
"io"
"sync/atomic"
)

func writeN(writer io.Writer, content []byte, n int) error {
Expand Down Expand Up @@ -47,6 +48,9 @@ func (c *Conn) WritePong(payload []byte) {
// text message must be utf8 encoding
// 发送文本/二进制消息, 文本消息必须是utf8编码
func (c *Conn) WriteMessage(opcode Opcode, payload []byte) {
if atomic.LoadUint32(&c.closed) == 1 {
return
}
c.emitError(c.writeMessage(opcode, payload))
}

Expand Down

0 comments on commit 8c22307

Please sign in to comment.