| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"net" |
| 6 |
"sync" |
| 7 |
|
| 8 |
"github.com/centrifugal/centrifuge" |
| 9 |
"github.com/gobwas/ws" |
| 10 |
"github.com/gobwas/ws/wsutil" |
| 11 |
) |
| 12 |
|
| 13 |
const websocketTransportName = "websocket" |
| 14 |
|
| 15 |
type customWebsocketTransport struct { |
| 16 |
mu sync.RWMutex |
| 17 |
closed bool |
| 18 |
closeCh chan struct{} |
| 19 |
|
| 20 |
conn net.Conn |
| 21 |
protoType centrifuge.ProtocolType |
| 22 |
} |
| 23 |
|
| 24 |
func newWebsocketTransport(conn net.Conn, protoType centrifuge.ProtocolType) *customWebsocketTransport { |
| 25 |
return &customWebsocketTransport{ |
| 26 |
conn: conn, |
| 27 |
protoType: protoType, |
| 28 |
closeCh: make(chan struct{}), |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// Name implementation. |
| 33 |
func (t *customWebsocketTransport) Name() string { |
| 34 |
return websocketTransportName |
| 35 |
} |
| 36 |
|
| 37 |
// Protocol implementation. |
| 38 |
func (t *customWebsocketTransport) Protocol() centrifuge.ProtocolType { |
| 39 |
return t.protoType |
| 40 |
} |
| 41 |
|
| 42 |
// Encoding implementation. |
| 43 |
func (t *customWebsocketTransport) Encoding() centrifuge.EncodingType { |
| 44 |
return centrifuge.EncodingTypeJSON |
| 45 |
} |
| 46 |
|
| 47 |
func (t *customWebsocketTransport) read() ([]byte, bool, error) { |
| 48 |
t.mu.Lock() |
| 49 |
defer t.mu.Unlock() |
| 50 |
|
| 51 |
h, r, err := wsutil.NextReader(t.conn, ws.StateServerSide) |
| 52 |
if err != nil { |
| 53 |
return nil, false, err |
| 54 |
} |
| 55 |
if h.OpCode.IsControl() { |
| 56 |
return nil, true, wsutil.ControlFrameHandler(t.conn, ws.StateServerSide)(h, r) |
| 57 |
} |
| 58 |
|
| 59 |
data, err := ioutil.ReadAll(r) |
| 60 |
if err != nil { |
| 61 |
return nil, false, err |
| 62 |
} |
| 63 |
|
| 64 |
return data, false, nil |
| 65 |
} |
| 66 |
|
| 67 |
func (t *customWebsocketTransport) Write(data []byte) error { |
| 68 |
select { |
| 69 |
case <-t.closeCh: |
| 70 |
return nil |
| 71 |
default: |
| 72 |
messageType := ws.OpText |
| 73 |
|
| 74 |
if t.Protocol() == centrifuge.ProtocolTypeProtobuf { |
| 75 |
messageType = ws.OpBinary |
| 76 |
} |
| 77 |
|
| 78 |
if err := wsutil.WriteServerMessage(t.conn, messageType, data); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
return nil |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Close implementation. |
| 87 |
func (t *customWebsocketTransport) Close(disconnect *centrifuge.Disconnect) error { |
| 88 |
t.mu.Lock() |
| 89 |
if t.closed { |
| 90 |
t.mu.Unlock() |
| 91 |
return nil |
| 92 |
} |
| 93 |
t.closed = true |
| 94 |
close(t.closeCh) |
| 95 |
t.mu.Unlock() |
| 96 |
|
| 97 |
if disconnect != nil { |
| 98 |
data := ws.NewCloseFrameBody(ws.StatusCode(disconnect.Code), disconnect.CloseText()) |
| 99 |
_ = wsutil.WriteServerMessage(t.conn, ws.OpClose, data) |
| 100 |
return t.conn.Close() |
| 101 |
} |
| 102 |
data := ws.NewCloseFrameBody(ws.StatusNormalClosure, "") |
| 103 |
_ = wsutil.WriteServerMessage(t.conn, ws.OpClose, data) |
| 104 |
return t.conn.Close() |
| 105 |
} |