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

Beta486 #491

Merged
merged 42 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a6ebe57
beta447
Hoshinonyaruko Jun 29, 2024
1d63fae
beta448
Hoshinonyaruko Jul 1, 2024
cd34627
beta449
Hoshinonyaruko Jul 4, 2024
b790b29
beta450
Hoshinonyaruko Jul 4, 2024
7a9b56a
beta451
Hoshinonyaruko Jul 4, 2024
e17d0d4
beta452
Hoshinonyaruko Jul 4, 2024
1318175
beta453
Hoshinonyaruko Jul 4, 2024
b7f291c
beta454
Hoshinonyaruko Jul 4, 2024
f921895
beta455
Hoshinonyaruko Jul 9, 2024
24108c6
btea455
Hoshinonyaruko Jul 9, 2024
7cdc968
beta456
Hoshinonyaruko Jul 9, 2024
b5d784e
beta457
Hoshinonyaruko Jul 9, 2024
61e4001
beta458
Hoshinonyaruko Jul 9, 2024
6d4116a
beta460
Hoshinonyaruko Jul 9, 2024
2c1f1b7
beta460
Hoshinonyaruko Jul 11, 2024
5c312cd
beta461
Hoshinonyaruko Jul 12, 2024
58304eb
beta462
Hoshinonyaruko Jul 13, 2024
7e7c9f3
beta463
Hoshinonyaruko Jul 15, 2024
188f250
beta464
Hoshinonyaruko Jul 15, 2024
af0296d
beta465
Hoshinonyaruko Jul 18, 2024
500135c
beta467
Hoshinonyaruko Jul 21, 2024
c1a588c
beta468
Hoshinonyaruko Jul 21, 2024
1df8e15
beta469
Hoshinonyaruko Jul 21, 2024
f57ab7b
beta470
Hoshinonyaruko Jul 26, 2024
cd21ad9
beta471
Hoshinonyaruko Aug 1, 2024
6039272
beta472
Hoshinonyaruko Aug 1, 2024
c45b450
beta473
Hoshinonyaruko Aug 1, 2024
f89d6c5
beta473
Hoshinonyaruko Aug 3, 2024
b54fdee
beta475
Hoshinonyaruko Aug 5, 2024
a30a697
beta476
Hoshinonyaruko Aug 8, 2024
ab805cc
beta478
Hoshinonyaruko Aug 8, 2024
6debefd
beta479
Hoshinonyaruko Aug 17, 2024
9cc7e5d
beta479
Hoshinonyaruko Aug 17, 2024
c0fe93c
beta480
Hoshinonyaruko Aug 18, 2024
91cf223
beta481
Hoshinonyaruko Aug 18, 2024
a3aa6c1
beta482
Hoshinonyaruko Aug 18, 2024
0917e56
beta483
Hoshinonyaruko Aug 19, 2024
dd1f774
beta484
Hoshinonyaruko Aug 19, 2024
1c87d61
beta485
Hoshinonyaruko Aug 20, 2024
aac474a
beta486
Hoshinonyaruko Aug 20, 2024
2604eb7
beta486
Hoshinonyaruko Aug 20, 2024
9559b24
Merge branch 'main' into beta486
Hoshinonyaruko Aug 20, 2024
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
Empty file added go
Empty file.
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,29 +747,39 @@ func ThreadEventHandler() event.ThreadEventHandler {
// GroupATMessageEventHandler 实现处理 群at 消息的回调
func GroupATMessageEventHandler() event.GroupATMessageEventHandler {
return func(event *dto.WSPayload, data *dto.WSGroupATMessageData) error {
botstats.RecordMessageReceived()
go p.ProcessGroupMessage(data)

if !config.GetDisableErrorChan() {
botstats.RecordMessageReceived()
}

if config.GetEnableChangeWord() {
data.Content = acnode.CheckWordIN(data.Content)
if data.Author.Username != "" {
data.Author.Username = acnode.CheckWordIN(data.Author.Username)
}
}
go p.ProcessGroupMessage(data)

return nil
}
}

// C2CMessageEventHandler 实现处理 群私聊 消息的回调
func C2CMessageEventHandler() event.C2CMessageEventHandler {
return func(event *dto.WSPayload, data *dto.WSC2CMessageData) error {
botstats.RecordMessageReceived()
go p.ProcessC2CMessage(data)

if !config.GetDisableErrorChan() {
botstats.RecordMessageReceived()
}

if config.GetEnableChangeWord() {
data.Content = acnode.CheckWordIN(data.Content)
if data.Author.Username != "" {
data.Author.Username = acnode.CheckWordIN(data.Author.Username)
}
}
go p.ProcessC2CMessage(data)

return nil
}
}
Expand Down
66 changes: 45 additions & 21 deletions wsclient/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"sync"
"time"

"github.com/gorilla/websocket"
Expand All @@ -24,36 +24,63 @@ type WebSocketClient struct {
botID uint64
urlStr string
cancel context.CancelFunc
mutex sync.Mutex // 用于同步写入和重连操作的互斥锁
isReconnecting bool
sendFailures []map[string]interface{} // 存储失败的消息
writeCh chan writeRequest // 写请求通道
closeCh chan struct{} // 用于关闭的通道
}

type writeRequest struct {
messageType int
data []byte
}

// 发送json信息给onebot应用端
// SendMessage 发送消息,将写请求发送到写 Goroutine
func (client *WebSocketClient) SendMessage(message map[string]interface{}) error {
client.mutex.Lock() // 在写操作之前锁定
defer client.mutex.Unlock() // 确保在函数返回时解锁

// 序列化消息
msgBytes, err := json.Marshal(message)
if err != nil {
mylog.Println("Error marshalling message:", err)
log.Println("Error marshalling message:", err)
return err
}

err = client.conn.WriteMessage(websocket.TextMessage, msgBytes)
if err != nil {
mylog.Println("Error sending message:", err)
// 发送失败,将消息添加到切片
if !config.GetDisableErrorChan() {
client.sendFailures = append(client.sendFailures, message)
}
return err
// 创建错误通道,用于接收写操作的结果
client.writeCh <- writeRequest{
messageType: websocket.TextMessage,
data: msgBytes,
}

// 等待写操作完成,并返回结果
return nil
}

// Close 关闭 WebSocketClient,停止写 Goroutine
func (client *WebSocketClient) Close() error {
close(client.closeCh)
close(client.writeCh)
client.conn.Close()
return nil
}

// startWriter 专用的写 Goroutine
func (client *WebSocketClient) startWriter() {
for {
select {
case req := <-client.writeCh:
// 执行写操作
err := client.conn.WriteMessage(req.messageType, req.data)
if err != nil {
log.Println("Error sending message:", err)
if !config.GetDisableErrorChan() {
client.sendFailures = append(client.sendFailures, map[string]interface{}{"message": req.data}) // 记录失败的消息
}
}
case <-client.closeCh:
return
}
}
}

// 处理onebotv11应用端发来的信息
func (client *WebSocketClient) handleIncomingMessages(cancel context.CancelFunc) {
for {
Expand Down Expand Up @@ -314,7 +341,10 @@ func NewWebSocketClient(urlStr string, botID uint64, api openapi.OpenAPI, apiv2
botID: botID,
urlStr: urlStr,
sendFailures: []map[string]interface{}{},
writeCh: make(chan writeRequest, 5000), // 缓冲区大小可以根据需求调整
closeCh: make(chan struct{}),
}
go client.startWriter() // 启动写 Goroutine

// Sending initial message similar to your setupB function
message := map[string]interface{}{
Expand Down Expand Up @@ -344,12 +374,6 @@ func NewWebSocketClient(urlStr string, botID uint64, api openapi.OpenAPI, apiv2
return client, nil
}

func (ws *WebSocketClient) Close() error {
ws.mutex.Lock()
defer ws.mutex.Unlock()
return ws.conn.Close()
}

// getParamsFromURI 解析给定URI中的查询参数,并返回一个映射(map)
func getParamsFromURI(uriStr string) map[string]string {
params := make(map[string]string)
Expand Down
Loading