Skip to content

Commit

Permalink
feat: format message type
Browse files Browse the repository at this point in the history
Signed-off-by: rfyiamcool <rfyiamcool@163.com>
  • Loading branch information
rfyiamcool authored and AlexVulaj committed Feb 14, 2024
1 parent 3168614 commit 0cfb2ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,3 +1263,15 @@ func FormatCloseMessage(closeCode int, text string) []byte {
copy(buf[2:], text)
return buf
}

var messageTypes = map[int]string{
TextMessage: "TextMessage",
BinaryMessage: "BinaryMessage",
CloseMessage: "CloseMessage",
PingMessage: "PingMessage",
PongMessage: "PongMessage",
}

func FormatMessageType(mt int) string {
return messageTypes[mt]
}
17 changes: 17 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,20 @@ func TestFailedConnectionReadPanic(t *testing.T) {
}
t.Fatal("should not get here")
}

func TestFormatMessageType(t *testing.T) {
str := FormatMessageType(TextMessage)
if str != messageTypes[TextMessage] {
t.Error("failed to format message type")
}

str = FormatMessageType(CloseMessage)
if str != messageTypes[CloseMessage] {
t.Error("failed to format message type")
}

str = FormatMessageType(123)
if str != messageTypes[123] {
t.Error("failed to format message type")
}
}

0 comments on commit 0cfb2ca

Please sign in to comment.