/
bot.go
69 lines (60 loc) · 1.53 KB
/
bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package server
import (
"fmt"
"io"
"sync/atomic"
"time"
)
// NewBot 创建一个机器人,目前仅支持 Socket 服务器
func NewBot(srv *Server, options ...BotOption) *Bot {
if !srv.IsSocket() {
panic(fmt.Errorf("server type[%s] is not socket", srv.network))
}
bot := &Bot{
conn: newBotConn(srv),
}
for _, option := range options {
option(bot)
}
return bot
}
type Bot struct {
conn *Conn
joined atomic.Bool
}
// JoinServer 加入服务器
func (slf *Bot) JoinServer() {
if slf.joined.Swap(true) {
slf.conn.server.OnConnectionClosedEvent(slf.conn, nil)
}
slf.conn.server.OnConnectionOpenedEvent(slf.conn)
}
// LeaveServer 离开服务器
func (slf *Bot) LeaveServer() {
if slf.joined.Swap(false) {
slf.conn.server.OnConnectionClosedEvent(slf.conn, nil)
}
}
// SetNetworkDelay 设置网络延迟和波动范围
// - delay 延迟
// - fluctuation 波动范围
func (slf *Bot) SetNetworkDelay(delay, fluctuation time.Duration) {
slf.conn.delay = delay
slf.conn.fluctuation = fluctuation
}
// SetWriter 设置写入器
func (slf *Bot) SetWriter(writer io.Writer) {
slf.conn.botWriter.Store(&writer)
}
// SendPacket 发送数据包到服务器
func (slf *Bot) SendPacket(packet []byte) {
if slf.conn.server.IsOnline(slf.conn.GetID()) {
slf.conn.server.PushPacketMessage(slf.conn, 0, packet)
}
}
// SendWSPacket 发送 WebSocket 数据包到服务器
func (slf *Bot) SendWSPacket(wst int, packet []byte) {
if slf.conn.server.IsOnline(slf.conn.GetID()) {
slf.conn.server.PushPacketMessage(slf.conn, wst, packet)
}
}