|
@@ -168,10 +168,10 @@ func (h *Hub) ConnectRange(fn func(id int, line *Line) bool) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取当前在线的数量
|
|
// 获取当前在线的数量
|
|
|
-func (h *Hub) ConnectedNum() int {
|
|
|
|
|
|
|
+func (h *Hub) ConnectNum(state ConnectState) int {
|
|
|
var count int
|
|
var count int
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == StateConnected {
|
|
|
|
|
|
|
+ if line.state == state {
|
|
|
count++
|
|
count++
|
|
|
}
|
|
}
|
|
|
return true
|
|
return true
|
|
@@ -180,10 +180,10 @@ func (h *Hub) ConnectedNum() int {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取所有的在线连接频道
|
|
// 获取所有的在线连接频道
|
|
|
-func (h *Hub) AllConnectedChannel() []string {
|
|
|
|
|
|
|
+func (h *Hub) AllConnectChannel(state ConnectState) []string {
|
|
|
cs := make([]string, 0)
|
|
cs := make([]string, 0)
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == StateConnected {
|
|
|
|
|
|
|
+ if line.state == state {
|
|
|
cs = append(cs, line.channel)
|
|
cs = append(cs, line.channel)
|
|
|
}
|
|
}
|
|
|
return true
|
|
return true
|
|
@@ -193,10 +193,10 @@ func (h *Hub) AllConnectedChannel() []string {
|
|
|
|
|
|
|
|
// 获取所有连接频道和连接时长
|
|
// 获取所有连接频道和连接时长
|
|
|
// 为了避免定义数据结构麻烦,采用|隔开, 频道名|连接开始时间
|
|
// 为了避免定义数据结构麻烦,采用|隔开, 频道名|连接开始时间
|
|
|
-func (h *Hub) AllConnectedChannelWithStarted() []string {
|
|
|
|
|
|
|
+func (h *Hub) AllConnectChannelWithStarted(state ConnectState) []string {
|
|
|
cs := make([]string, 0)
|
|
cs := make([]string, 0)
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == StateConnected {
|
|
|
|
|
|
|
+ if line.state == state {
|
|
|
cs = append(cs, fmt.Sprintf("%s|%d", line.channel, line.started.UnixMilli()))
|
|
cs = append(cs, fmt.Sprintf("%s|%d", line.channel, line.started.UnixMilli()))
|
|
|
}
|
|
}
|
|
|
return true
|
|
return true
|
|
@@ -205,9 +205,9 @@ func (h *Hub) AllConnectedChannelWithStarted() []string {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取频道并通过函数过滤,如果返回 false 将终止
|
|
// 获取频道并通过函数过滤,如果返回 false 将终止
|
|
|
-func (h *Hub) ConnectedChannelToFunc(fn func(string) bool) {
|
|
|
|
|
|
|
+func (h *Hub) ConnectChannelToFunc(state ConnectState, fn func(string) bool) {
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == StateConnected {
|
|
|
|
|
|
|
+ if line.state == state {
|
|
|
return fn(line.channel)
|
|
return fn(line.channel)
|
|
|
}
|
|
}
|
|
|
return true
|
|
return true
|
|
@@ -258,7 +258,7 @@ func (h *Hub) sendRequest(gd *GetData) (count int, err error) {
|
|
|
// 发送数据到网络
|
|
// 发送数据到网络
|
|
|
doit := func(_ int, line *Line) bool {
|
|
doit := func(_ int, line *Line) bool {
|
|
|
// 检查连接是否OK
|
|
// 检查连接是否OK
|
|
|
- if line.state != StateConnected {
|
|
|
|
|
|
|
+ if line.state != StateConnected && line.state != StateProxied {
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
// 验证连接是否达到发送数据的要求
|
|
// 验证连接是否达到发送数据的要求
|
|
@@ -284,7 +284,7 @@ func (h *Hub) sendRequest(gd *GetData) (count int, err error) {
|
|
|
// 检查是否已经很久时间没有使用连接了
|
|
// 检查是否已经很久时间没有使用连接了
|
|
|
if time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*3*int(time.Millisecond)) {
|
|
if time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*3*int(time.Millisecond)) {
|
|
|
// 超时关闭当前的连接
|
|
// 超时关闭当前的连接
|
|
|
- log.Println("get message timeout", conn.channel)
|
|
|
|
|
|
|
+ log.Println("get message timeout error:", conn.channel)
|
|
|
// 有可能连接出现问题,断开并重新连接
|
|
// 有可能连接出现问题,断开并重新连接
|
|
|
conn.Close(false)
|
|
conn.Close(false)
|
|
|
return
|
|
return
|
|
@@ -697,7 +697,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
// 检查当前channel是否已经存在
|
|
// 检查当前channel是否已经存在
|
|
|
if !force {
|
|
if !force {
|
|
|
line := h.ChannelToLine(remoteChannel)
|
|
line := h.ChannelToLine(remoteChannel)
|
|
|
- if line != nil && line.state == StateConnected {
|
|
|
|
|
|
|
+ if line != nil && (line.state == StateConnected || line.state == StateProxied) {
|
|
|
log.Println("[ConnectToServer] channel existed:", remoteChannel)
|
|
log.Println("[ConnectToServer] channel existed:", remoteChannel)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -858,7 +858,7 @@ func (h *Hub) checkConnect() {
|
|
|
func (h *Hub) Quit() {
|
|
func (h *Hub) Quit() {
|
|
|
h.cancel()
|
|
h.cancel()
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == StateConnected {
|
|
|
|
|
|
|
+ if line.state == StateConnected || line.state == StateProxied {
|
|
|
line.Close(true)
|
|
line.Close(true)
|
|
|
}
|
|
}
|
|
|
return true
|
|
return true
|