Rejoin channels after reconnecting

This commit is contained in:
Ken-Håvard Lieng 2016-02-03 21:12:32 +01:00
parent 90b74ee022
commit 875f5fafe3
3 changed files with 35 additions and 1 deletions

View File

@ -22,7 +22,8 @@ type Client struct {
Messages chan *Message
ConnectionChanged chan bool
nick string
nick string
channels []string
conn net.Conn
connected bool
@ -149,3 +150,18 @@ func (c *Client) register() {
c.writeNick(c.nick)
c.writeUser(c.Username, c.Realname)
}
func (c *Client) addChannel(channel string) {
c.lock.Lock()
c.channels = append(c.channels, channel)
c.lock.Unlock()
}
func (c *Client) flushChannels() {
c.lock.Lock()
if len(c.channels) > 0 {
c.Join(c.channels...)
c.channels = []string{}
}
c.lock.Unlock()
}

View File

@ -152,3 +152,14 @@ func TestRegister(t *testing.T) {
assert.Equal(t, "NICK nick\r\n", <-out)
assert.Equal(t, "USER user 0 * :rn\r\n", <-out)
}
func TestFlushChannels(t *testing.T) {
c, out := testClientSend()
c.addChannel("#chan1")
c.flushChannels()
assert.Equal(t, <-out, "JOIN #chan1\r\n")
c.addChannel("#chan2")
c.addChannel("#chan3")
c.flushChannels()
assert.Equal(t, <-out, "JOIN #chan2,#chan3\r\n")
}

View File

@ -93,6 +93,8 @@ func (c *Client) tryConnect() {
err := c.connect()
if err == nil {
c.backoff.Reset()
c.flushChannels()
return
}
@ -179,6 +181,11 @@ func (c *Client) recv() {
case Ping:
go c.write("PONG :" + msg.Trailing)
case Join:
if msg.Nick == c.GetNick() {
c.addChannel(msg.Params[0])
}
case ReplyWelcome:
c.once.Do(c.ready.Done)
}