-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,827 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cqhttp_positive_ws_driver | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
type Bot struct { | ||
selfId int64 | ||
conn *websocket.Conn | ||
responses sync.Map | ||
lock sync.Mutex | ||
disConnectHandle func(selfId int64) | ||
} | ||
|
||
// GetSelfId | ||
/** | ||
* @Description: 获取bot的账号 | ||
* @receiver b | ||
* @return int64 | ||
* example | ||
*/ | ||
func (b *Bot) GetSelfId() int64 { | ||
return b.selfId | ||
} | ||
|
||
// Do | ||
/** | ||
* @Description: 执行一个api的调用 | ||
* @receiver b | ||
* @param i | ||
* example | ||
*/ | ||
func (b *Bot) Do(i interface{}) { | ||
err := b.conn.WriteJSON(i) | ||
if err != nil { | ||
b.wsClose() | ||
return | ||
} | ||
} | ||
|
||
// GetResponse | ||
/** | ||
* @Description: 获取一个api调用的响应 | ||
* @receiver b | ||
* @param echo api调用的唯一标识 | ||
* @return []byte | ||
* @return error | ||
* example | ||
*/ | ||
func (b *Bot) GetResponse(echo string) ([]byte, error) { | ||
defer func() { | ||
b.responses.Delete(echo) | ||
}() | ||
|
||
for i := 0; i < 120; i++ { | ||
value, ok := b.responses.LoadAndDelete(echo) | ||
if ok { | ||
return value.([]byte), nil | ||
} | ||
time.Sleep(500) | ||
} | ||
|
||
return nil, errors.New("get response time out") | ||
} | ||
|
||
func (b *Bot) wsClose() { | ||
_ = b.conn.Close() | ||
b.lock.Lock() | ||
b.disConnectHandle(b.selfId) | ||
defer b.lock.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package cqhttp_positive_ws_driver | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"sync" | ||
|
||
"github.com/gorilla/websocket" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
type Driver struct { | ||
Name string | ||
address string | ||
port int | ||
bots sync.Map | ||
eventChan chan []byte | ||
connectHandle func(selfId int64, host string, clientRole string) | ||
disConnectHandle func(selfId int64) | ||
} | ||
|
||
func (d *Driver) Run() { | ||
u := url.URL{Scheme: "ws", Host: d.address + ":" + strconv.Itoa(d.port)} | ||
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) | ||
if err != nil { | ||
return | ||
} | ||
_, data, err := conn.ReadMessage() | ||
if err != nil { | ||
return | ||
} | ||
selfId := gjson.GetBytes(data, "self_id").Int() | ||
role := "" | ||
host := d.address | ||
|
||
b := new(Bot) | ||
b.conn = conn | ||
b.selfId = selfId | ||
b.responses = sync.Map{} | ||
|
||
_, ok := d.bots.Load(selfId) | ||
if ok { | ||
d.bots.LoadOrStore(selfId, b) | ||
} else { | ||
d.bots.Store(selfId, b) | ||
} | ||
|
||
d.connectHandle(selfId, host, role) | ||
b.disConnectHandle = d.disConnectHandle | ||
log.Infoln(fmt.Sprintf("the bot %v is connected", selfId)) | ||
go func() { | ||
defer func() { | ||
i := recover() | ||
if i != nil { | ||
log.Errorln("ws链接读取出现错误") | ||
log.Errorln(i) | ||
d.disConnectHandle(selfId) | ||
} | ||
}() | ||
for { | ||
_, data, err := conn.ReadMessage() | ||
if err != nil { | ||
b.wsClose() | ||
} | ||
|
||
echo := gjson.GetBytes(data, "echo") | ||
if echo.Exists() { | ||
b.responses.Store(echo.String(), data) | ||
} else { | ||
d.eventChan <- data | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (d *Driver) GetEvent() chan []byte { | ||
return d.eventChan | ||
} | ||
|
||
func (d *Driver) GetBot(i int64) interface{} { | ||
load, ok := d.bots.Load(i) | ||
if ok { | ||
return load | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// OnConnect | ||
/** | ||
* @Description: | ||
* @receiver d | ||
* @param f | ||
* example | ||
*/ | ||
func (d *Driver) OnConnect(f func(selfId int64, host string, clientRole string)) { | ||
d.connectHandle = f | ||
} | ||
|
||
// OnDisConnect | ||
/** | ||
* @Description: | ||
* @receiver d | ||
* @param f | ||
* example | ||
*/ | ||
func (d *Driver) OnDisConnect(f func(selfId int64)) { | ||
d.disConnectHandle = f | ||
} | ||
|
||
// GetBots | ||
/** | ||
* @Description: | ||
* @receiver d | ||
* @return map[int64]interface{} | ||
* example | ||
*/ | ||
func (d *Driver) GetBots() map[int64]interface{} { | ||
m := make(map[int64]interface{}) | ||
d.bots.Range(func(key, value interface{}) bool { | ||
m[key.(int64)] = value | ||
return true | ||
}) | ||
|
||
return m | ||
} | ||
|
||
// SetAddress | ||
/** | ||
* @Description: | ||
* @receiver d | ||
* @param string2 | ||
* example | ||
*/ | ||
func (d *Driver) SetAddress(string2 string) { | ||
d.address = string2 | ||
} | ||
|
||
func (d *Driver) SetPort(port int) { | ||
d.port = port | ||
} | ||
|
||
func NewDriver() *Driver { | ||
d := new(Driver) | ||
d.Name = "cqhttp" | ||
d.bots = sync.Map{} | ||
d.eventChan = make(chan []byte) | ||
return d | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cqhttp_ws_driver | ||
package cqhttp_reverse_ws_driver | ||
|
||
import ( | ||
"encoding/json" //nolint:gci | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cqhttp_ws_driver | ||
package cqhttp_reverse_ws_driver | ||
|
||
import ( | ||
"errors" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters