Skip to content

Commit

Permalink
mod:完成事件全部内容
Browse files Browse the repository at this point in the history
  • Loading branch information
dezhiShen authored and dezhiShen committed Oct 15, 2023
1 parent 3978815 commit 1dc7eca
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 44 deletions.
12 changes: 6 additions & 6 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ func main() {
Api: config.SatoriApiConfig{
Type: "http",
Platform: "red",
SelfId: "123456",
Endpoint: "http://127.0.0.1:5140",
SelfId: "11123456789",
Endpoint: "http://127.0.0.1:5140", //https://
},
Event: config.SatoriEventConfig{
Type: "webhook",
Addr: "0.0.0.0:8080",
Type: "websocket",
Addr: "ws://127.0.0.1:5140", // wss://
},
// Event: config.SatoriEventConfig{
// Type: "websocket",
// Addr: "http://127.0.0.1:5140",
// Type: "webhook",
// Addr: "0.0.0.0:8080", // 客户端服务的地址,127.0.0.1:8080 限制访问
// },
}
satoriApi, err := api.NewSatorApiByConfig(conf)
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
Expand All @@ -41,6 +43,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.17.0
github.com/tidwall/gjson v1.17.0
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
Expand Down
16 changes: 0 additions & 16 deletions pkg/client/constant.go

This file was deleted.

1 change: 1 addition & 0 deletions pkg/client/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

type EventTemplate interface {
SetSequence(sequence int64)
StartListen(context.Context, func(message []byte) error) error
// callback func(message []byte) error
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/event_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func NewWebhookEventChannel(conf config.SatoriEventConfig) (EventTemplate, error
return result, nil
}

func (*WebhookEventChannel) SetSequence(sequence int64) {

}

func (cli *WebhookEventChannel) StartListen(ctx context.Context, handler func(message []byte) error) error {
errChan := make(chan error)
cli.handler = handler
Expand Down
23 changes: 16 additions & 7 deletions pkg/client/event_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import (
"context"
"fmt"
"strings"
"sync"
"time"

"github.com/dezhishen/satori-sdk-go/pkg/config"
"github.com/dezhishen/satori-sdk-go/pkg/constant"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
)

type WebsocketEventChannel struct {
mu sync.Mutex
Conn *websocket.Conn
addr string
accessToken string
sequence string
sequence int64
}

func NewWebsocketEventChannel(conf config.SatoriEventConfig) (EventTemplate, error) {
Expand All @@ -32,12 +35,16 @@ func NewWebsocketEventChannel(conf config.SatoriEventConfig) (EventTemplate, err
}
return result, nil
}

func (cli *WebsocketEventChannel) SetSequence(sequence int64) {
cli.sequence = sequence
}
func (cli *WebsocketEventChannel) sendIDENTIFY() {
defer cli.mu.Unlock()
cli.mu.Lock()
log.Info("send IDENTIFY")
err := cli.Conn.WriteJSON(map[string]interface{}{
"op": IDENTIFY,
"body": map[string]string{
"op": constant.SIGN_NUM_IDENTIFY,
"body": map[string]interface{}{
"token": cli.accessToken,
"sequence": cli.sequence,
},
Expand All @@ -55,17 +62,19 @@ func (cli *WebsocketEventChannel) startHeartbeat() {
}

func (cli *WebsocketEventChannel) sendPING() {
log.Info("send IDENTIFY")
defer cli.mu.Unlock()
cli.mu.Lock()
log.Info("send PING")
err := cli.Conn.WriteJSON(map[string]interface{}{
"op": PING,
"op": constant.SIGN_NUM_PING,
})
if err != nil {
log.Errorf("PING发送失败:%v", err)
}
}

func (cli *WebsocketEventChannel) StartListen(ctx context.Context, callback func(message []byte) error) error {
url := cli.addr
url := cli.addr + "/events"
if cli.accessToken != "" {
url = fmt.Sprintf("%s?access_token=%s", url, cli.accessToken)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/constant/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package constant

const (
// 单位: ms
DEFAULT_TIMEOUT = 10000
)

type SignNum uint64

const (
SIGN_NUM_EVENT SignNum = 0
SIGN_NUM_PING SignNum = 1
SIGN_NUM_PONG SignNum = 2
SIGN_NUM_IDENTIFY SignNum = 3
SIGN_NUM_READY SignNum = 4
)
40 changes: 30 additions & 10 deletions pkg/event/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

"github.com/dezhishen/satori-sdk-go/pkg/client"
"github.com/dezhishen/satori-sdk-go/pkg/config"
"github.com/dezhishen/satori-sdk-go/pkg/constant"
"github.com/dezhishen/satori-sdk-go/pkg/resource/login"
"github.com/tidwall/gjson"
)

type SatoriEvent interface {
Expand Down Expand Up @@ -37,18 +40,35 @@ type SatoriEventImpl struct {

func (s *SatoriEventImpl) StartWithCtx(ctx context.Context) error {
return s.template.StartListen(ctx, func(message []byte) error {
var e Event
err := json.Unmarshal(message, &e)
if err != nil {
return fmt.Errorf("handle listen decoder err :%v,raw:%v", err, message)
}
handler, ok := s.allHandler[e.Type]
if ok {
for _, callback := range handler {
go runCallbakc(e, callback)
op := gjson.GetBytes(message, "op").Uint()
switch constant.SignNum(op) {
case constant.SIGN_NUM_EVENT:
var e Event
body := gjson.GetBytes(message, "body").Raw
err := json.Unmarshal([]byte(body), &e)
if err != nil {
return fmt.Errorf("handle listen decoder err :%v,raw:%v", err, message)
}
log.Infof("recive EVENT: %s", e.Type)
s.template.SetSequence(e.Id)
handler, ok := s.allHandler[e.Type]
if ok {
for _, callback := range handler {
go runCallbakc(e, callback)
}
}
return nil
case constant.SIGN_NUM_PONG:
log.Infof("recive PONG")
return nil
case constant.SIGN_NUM_READY:
var logins []login.Login
_ = json.Unmarshal([]byte(gjson.GetBytes(message, "body").Raw), &logins)
log.Infof("recive READY:%v", logins)
return nil
default:
return fmt.Errorf("unsupport signNum %d", op)
}
return nil
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/event/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type SignInfo struct {
Op uint8 `json:"op"`
Op uint64 `json:"op"`
Body interface{} `json:"body,omitempty"`
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/resource/channel/model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package channel

type ChannelType uint64

const (
CHANNEL_TYPE_TEXT ChannelType = 0
CHANNEL_TYPE_VOICE ChannelType = 1
CHANNEL_TYPE_CATEGORY ChannelType = 2
CHANNEL_TYPE_DIRECT ChannelType = 3
)

type Channel struct {
Id string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
ParentId string `json:"parent_id"`
Id string `json:"id"`
Type ChannelType `json:"type"`
Name string `json:"name"`
ParentId string `json:"parent_id"`
}

type ChannelList struct {
Expand Down

0 comments on commit 1dc7eca

Please sign in to comment.