Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8ee76e4

Browse files
committedDec 31, 2020
new: TransferMode Binary/Text for Connection and hubProtocol
1 parent 7d4b65f commit 8ee76e4

6 files changed

+43
-3
lines changed
 

‎connection.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77
)
88

9-
// Connection describes a connection between signalR client and Server
9+
// Connection describes a connection between signalR client and server
1010
type Connection interface {
1111
io.Reader
1212
io.Writer
@@ -16,3 +16,18 @@ type Connection interface {
1616
Timeout() time.Duration
1717
SetTimeout(duration time.Duration)
1818
}
19+
20+
type TransferMode int
21+
22+
// MessageType constants.
23+
const (
24+
// TextTransferMode is for UTF-8 encoded text messages like JSON.
25+
TextTransferMode TransferMode = iota + 1
26+
// BinaryTransferMode is for binary messages like MessagePack.
27+
BinaryTransferMode
28+
)
29+
30+
type ConnectionWithTransferMode interface {
31+
TransferMode() TransferMode
32+
SetTransferMode(transferMode TransferMode)
33+
}

‎hubconnection.go

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func newHubConnection(connection Connection, protocol hubProtocol, maximumReceiv
4343
items: &sync.Map{},
4444
info: info,
4545
}
46+
if connectionWithTransferMode, ok := connection.(ConnectionWithTransferMode); ok {
47+
connectionWithTransferMode.SetTransferMode(protocol.transferMode())
48+
}
4649
return c
4750
}
4851

‎hubprotocol.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type hubProtocol interface {
1515
WriteMessage(message interface{}, writer io.Writer) error
1616
UnmarshalArgument(src interface{}, dst interface{}) error
1717
setDebugLogger(dbg StructuredLogger)
18+
transferMode() TransferMode
1819
}
1920

2021
//easyjson:json

‎jsonhubprotocol.go

+4
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ func (j *jsonHubProtocol) WriteMessage(message interface{}, writer io.Writer) er
209209
return fmt.Errorf("%#v does not implement json.Marshaler", message)
210210
}
211211

212+
func (j *jsonHubProtocol) transferMode() TransferMode {
213+
return TextTransferMode
214+
}
215+
212216
func (j *jsonHubProtocol) setDebugLogger(dbg StructuredLogger) {
213217
j.dbg = log.WithPrefix(dbg, "ts", log.DefaultTimestampUTC, "protocol", "JSON")
214218
}

‎messagepackhubprotocol.go

+4
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ func encodeMsgHeader(e *msgpack.Encoder, msgLen int, msgType int) (err error) {
300300
return nil
301301
}
302302

303+
func (m *messagePackHubProtocol) transferMode() TransferMode {
304+
return BinaryTransferMode
305+
}
306+
303307
func (m *messagePackHubProtocol) setDebugLogger(dbg StructuredLogger) {
304308
m.dbg = log.WithPrefix(dbg, "ts", log.DefaultTimestampUTC, "protocol", "MSGP")
305309
}

‎websocketconnection.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010

1111
type webSocketConnection struct {
1212
ConnectionBase
13-
conn *websocket.Conn
13+
conn *websocket.Conn
14+
transferMode TransferMode
1415
}
1516

1617
func newWebSocketConnection(parentContext context.Context, requestContext context.Context, connectionID string, conn *websocket.Conn) *webSocketConnection {
@@ -35,7 +36,11 @@ func (w *webSocketConnection) Write(p []byte) (n int, err error) {
3536
ctx, cancel = context.WithTimeout(w.ctx, w.Timeout())
3637
defer cancel() // has no effect because timeoutCtx is either done or not used anymore after websocket returns. But it keeps lint quiet
3738
}
38-
err = w.conn.Write(ctx, websocket.MessageText, p)
39+
messageType := websocket.MessageText
40+
if w.transferMode == BinaryTransferMode {
41+
messageType = websocket.MessageBinary
42+
}
43+
err = w.conn.Write(ctx, messageType, p)
3944
if err != nil {
4045
return 0, err
4146
}
@@ -58,3 +63,11 @@ func (w *webSocketConnection) Read(p []byte) (n int, err error) {
5863
}
5964
return bytes.NewReader(data).Read(p)
6065
}
66+
67+
func (w *webSocketConnection) TransferMode() TransferMode {
68+
return w.transferMode
69+
}
70+
71+
func (w *webSocketConnection) SetTransferMode(transferMode TransferMode) {
72+
w.transferMode = transferMode
73+
}

0 commit comments

Comments
 (0)