Add IRC connection status indicator
This commit is contained in:
parent
83aef5df7b
commit
f429a528ba
11 changed files with 124 additions and 38 deletions
|
@ -31,13 +31,21 @@ func newIRCHandler(client *irc.Client, session *Session) *ircHandler {
|
|||
|
||||
func (i *ircHandler) run() {
|
||||
for {
|
||||
msg, ok := <-i.client.Messages
|
||||
if !ok {
|
||||
i.session.deleteIRC(i.client.Host)
|
||||
return
|
||||
}
|
||||
select {
|
||||
case msg, ok := <-i.client.Messages:
|
||||
if !ok {
|
||||
i.session.deleteIRC(i.client.Host)
|
||||
return
|
||||
}
|
||||
|
||||
i.dispatchMessage(msg)
|
||||
i.dispatchMessage(msg)
|
||||
|
||||
case connected := <-i.client.ConnectionChanged:
|
||||
i.session.sendJSON("connection_update", map[string]bool{
|
||||
i.client.Host: connected,
|
||||
})
|
||||
i.session.setConnectionState(i.client.Host, connected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,8 +8,9 @@ import (
|
|||
)
|
||||
|
||||
type Session struct {
|
||||
irc map[string]*irc.Client
|
||||
ircLock sync.Mutex
|
||||
irc map[string]*irc.Client
|
||||
connectionState map[string]bool
|
||||
ircLock sync.Mutex
|
||||
|
||||
ws map[string]*wsConn
|
||||
wsLock sync.Mutex
|
||||
|
@ -20,9 +21,10 @@ type Session struct {
|
|||
|
||||
func NewSession() *Session {
|
||||
return &Session{
|
||||
irc: make(map[string]*irc.Client),
|
||||
ws: make(map[string]*wsConn),
|
||||
out: make(chan WSResponse, 32),
|
||||
irc: make(map[string]*irc.Client),
|
||||
connectionState: make(map[string]bool),
|
||||
ws: make(map[string]*wsConn),
|
||||
out: make(chan WSResponse, 32),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,12 +39,14 @@ func (s *Session) getIRC(server string) (*irc.Client, bool) {
|
|||
func (s *Session) setIRC(server string, i *irc.Client) {
|
||||
s.ircLock.Lock()
|
||||
s.irc[server] = i
|
||||
s.connectionState[server] = false
|
||||
s.ircLock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Session) deleteIRC(server string) {
|
||||
s.ircLock.Lock()
|
||||
delete(s.irc, server)
|
||||
delete(s.connectionState, server)
|
||||
s.ircLock.Unlock()
|
||||
}
|
||||
|
||||
|
@ -54,6 +58,24 @@ func (s *Session) numIRC() int {
|
|||
return n
|
||||
}
|
||||
|
||||
func (s *Session) getConnectionStates() map[string]bool {
|
||||
s.ircLock.Lock()
|
||||
state := make(map[string]bool, len(s.connectionState))
|
||||
|
||||
for k, v := range s.connectionState {
|
||||
state[k] = v
|
||||
}
|
||||
s.ircLock.Unlock()
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
func (s *Session) setConnectionState(server string, connected bool) {
|
||||
s.ircLock.Lock()
|
||||
s.connectionState[server] = connected
|
||||
s.ircLock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Session) setWS(addr string, w *wsConn) {
|
||||
s.wsLock.Lock()
|
||||
s.ws[addr] = w
|
||||
|
|
|
@ -71,6 +71,7 @@ func (h *wsHandler) init(uuid string) {
|
|||
|
||||
h.session.sendJSON("channels", channels)
|
||||
h.session.sendJSON("servers", h.session.user.GetServers())
|
||||
h.session.sendJSON("connection_update", h.session.getConnectionStates())
|
||||
|
||||
for _, channel := range channels {
|
||||
h.session.sendJSON("users", Userlist{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue