代码拉取完成,页面将自动刷新
package remotedialer
import (
"fmt"
"math/rand"
"sync"
"github.com/gorilla/websocket"
)
type sessionManager struct {
sync.Mutex
clients map[string][]*session
}
func newSessionManager() *sessionManager {
return &sessionManager{
clients: map[string][]*session{},
}
}
func (sm *sessionManager) getByClient(clientKey string) (*session, error) {
sm.Lock()
defer sm.Unlock()
sessions := sm.clients[clientKey]
if len(sessions) > 0 {
return sessions[0], nil
}
return nil, fmt.Errorf("failed to find session for client %s", clientKey)
}
func (sm *sessionManager) add(clientKey string, conn *websocket.Conn) *session {
sessionKey := rand.Int63()
session := newSession(sessionKey, clientKey, conn)
session.sessionKey = sessionKey
sm.Lock()
defer sm.Unlock()
sm.clients[clientKey] = append(sm.clients[clientKey], session)
return session
}
func (sm *sessionManager) remove(s *session) {
sm.Lock()
defer sm.Unlock()
var newSessions []*session
for _, v := range sm.clients[s.clientKey] {
if v.sessionKey == s.sessionKey {
continue
}
newSessions = append(newSessions, v)
}
if len(newSessions) == 0 {
delete(sm.clients, s.clientKey)
} else {
sm.clients[s.clientKey] = newSessions
}
s.Close()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。