Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support text ping pong #2829

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions internal/msggateway/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package msggateway

import (
"context"
"encoding/json"
"fmt"
"runtime/debug"
"sync"
Expand Down Expand Up @@ -159,9 +160,12 @@ func (c *Client) readMessage() {
return
}
case MessageText:
c.closedErr = ErrNotSupportMessageProtocol
return

_ = c.conn.SetReadDeadline(pongWait)
parseDataErr := c.handlerTextMessage(message)
if parseDataErr != nil {
c.closedErr = parseDataErr
return
}
case PingMessage:
err := c.writePongMsg("")
log.ZError(c.ctx, "writePongMsg", err)
Expand Down Expand Up @@ -419,3 +423,23 @@ func (c *Client) writePongMsg(appData string) error {

return errs.Wrap(err)
}

func (c *Client) handlerTextMessage(b []byte) error {
var msg TextMessage
if err := json.Unmarshal(b, &msg); err != nil {
return err
}
switch msg.Type {
case TextPong:
return nil
case TextPing:
msg.Type = TextPong
msgData, err := json.Marshal(msg)
if err != nil {
return err
}
return c.conn.WriteMessage(MessageText, msgData)
default:
return fmt.Errorf("not support message type %s", msg.Type)
}
}
11 changes: 11 additions & 0 deletions internal/msggateway/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package msggateway

import (
"context"
"encoding/json"
"sync"

"github.com/go-playground/validator/v10"
Expand All @@ -31,6 +32,16 @@ import (
"github.com/openimsdk/tools/utils/jsonutil"
)

const (
TextPing = "ping"
TextPong = "pong"
)

type TextMessage struct {
Type string `json:"type"`
Body json.RawMessage `json:"body"`
}

type Req struct {
ReqIdentifier int32 `json:"reqIdentifier" validate:"required"`
Token string `json:"token"`
Expand Down
Loading