Skip to content

Commit

Permalink
marking deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Sep 23, 2023
1 parent a6df1a8 commit 278b939
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func main() {
```go
func Broadcast(conns []*gws.Conn, opcode gws.Opcode, payload []byte) {
var b = gws.NewBroadcaster(opcode, payload)
defer b.Release()
defer b.Close()
for _, item := range conns {
_ = b.Broadcast(item)
}
Expand Down
9 changes: 8 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
)

type Conn struct {
SessionStorage SessionStorage // 会话
// 已废弃, 请使用Session()方法替代
// Deprecated: please use Session() method instead
SessionStorage SessionStorage // 会话

err atomic.Value // 错误
isServer bool // 是否为服务器
subprotocol string // 子协议
Expand Down Expand Up @@ -206,3 +209,7 @@ func (c *Conn) SetNoDelay(noDelay bool) error {
// SubProtocol 获取协商的子协议
// Get negotiated sub-protocols
func (c *Conn) SubProtocol() string { return c.subprotocol }

// Session 获取会话存储
// get session storage
func (c *Conn) Session() SessionStorage { return c.SessionStorage }
6 changes: 3 additions & 3 deletions examples/chatroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ type WebSocket struct {
}

func (c *WebSocket) getName(socket *gws.Conn) string {
name, _ := socket.SessionStorage.Load("name")
name, _ := socket.Session().Load("name")
return name.(string)
}

func (c *WebSocket) getKey(socket *gws.Conn) string {
name, _ := socket.SessionStorage.Load("key")
name, _ := socket.Session().Load("key")
return name.(string)
}

Expand All @@ -77,7 +77,7 @@ func (c *WebSocket) OnOpen(socket *gws.Conn) {
if conn, ok := c.sessions.Load(name); ok {
conn.WriteClose(1000, []byte("connection replaced"))
}
socket.SetDeadline(time.Now().Add(3 * PingInterval))
socket.SetDeadline(time.Now().Add(PingInterval + HeartbeatWaitTimeout))
c.sessions.Store(name, socket)
log.Printf("%s connected\n", name)
}
Expand Down
29 changes: 4 additions & 25 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"github.com/stretchr/testify/assert"
"hash/fnv"
"io"
"net/http"
"reflect"
"strings"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
)

func TestStringToBytes(t *testing.T) {
Expand Down Expand Up @@ -215,10 +214,9 @@ func TestGetIntersectionElem(t *testing.T) {

func TestResetBuffer(t *testing.T) {
{
var buf = bytes.NewBufferString("hello")
var p = AlphabetNumeric.Generate(1024)
BufferReset(buf, p)
assert.Equal(t, string(p), buf.String())
var buffer = bytes.NewBufferString("hello")
var name = reflect.TypeOf(buffer).Elem().Field(0).Name
assert.Equal(t, "buf", name)
}

{
Expand All @@ -231,23 +229,4 @@ func TestResetBuffer(t *testing.T) {
var sh2 = (*reflect.SliceHeader)(unsafe.Pointer(buf))
assert.Equal(t, sh1.Data, sh2.Data)
}

{
var buf = bytes.NewBufferString("hello")
buf.Next(2)
assert.Equal(t, "llo", buf.String())
BufferReset(buf, []byte("aha"))
assert.Equal(t, "a", buf.String())
assert.Equal(t, int64(2), reflect.ValueOf(buf).Elem().FieldByName("off").Int())
assert.Equal(t, int64(-1), reflect.ValueOf(buf).Elem().FieldByName("lastRead").Int())
}

{
var buf = bytes.NewBufferString("hello")
var p = buf.Bytes()
io.ReadAll(buf)
assert.Equal(t, "", buf.String())
BufferReset(buf, p)
assert.Equal(t, "hello", buf.String())
}
}
13 changes: 9 additions & 4 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,16 @@ func (c *Broadcaster) doClose() {
}
}

// Release 释放资源
// 在完成所有Broadcast调用之后执行Release方法释放资源.
// Call the Release method after all the Broadcasts have been completed to release the resources.
func (c *Broadcaster) Release() {
// Release 已废弃, 请使用Release方法替代
// Deprecated: please use Close() method instead
func (c *Broadcaster) Release() { _ = c.Close() }

// Close 释放资源
// 在完成所有Broadcast调用之后执行Close方法释放资源.
// Call the Close method after all the Broadcasts have been completed to release the resources.
func (c *Broadcaster) Close() error {
if atomic.AddInt64(&c.state, -1*math.MaxInt32) == 0 {
c.doClose()
}
return nil
}
7 changes: 7 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,16 @@ func TestNewBroadcaster(t *testing.T) {
app := NewServer(new(BuiltinEventHandler), &ServerOption{
CompressEnabled: true,
WriteMaxPayloadSize: 1000,
Authorize: func(r *http.Request, session SessionStorage) bool {
session.Store("name", 1)
session.Store("name", 2)
return true
},
})

app.OnRequest = func(socket *Conn, request *http.Request) {
name, _ := socket.Session().Load("name")
as.Equal(2, name)
handler.sockets.Store(socket, struct{}{})
socket.ReadLoop()
}
Expand Down

0 comments on commit 278b939

Please sign in to comment.