2016-09-05 14:34:37 +00:00
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
2018-12-01 18:55:35 +00:00
|
|
|
"net/url"
|
2018-08-09 22:38:19 +00:00
|
|
|
"sync"
|
2016-09-05 14:34:37 +00:00
|
|
|
"time"
|
|
|
|
|
2018-08-09 22:38:19 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2016-09-05 14:34:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MaxMessageTextLength is the current maximum message length in number of characters as defined here
|
|
|
|
// https://api.slack.com/rtm#limits
|
|
|
|
MaxMessageTextLength = 4000
|
|
|
|
)
|
|
|
|
|
|
|
|
// RTM represents a managed websocket connection. It also supports
|
|
|
|
// all the methods of the `Client` type.
|
|
|
|
//
|
2017-07-16 12:29:46 +00:00
|
|
|
// Create this element with Client's NewRTM() or NewRTMWithOptions(*RTMOptions)
|
2016-09-05 14:34:37 +00:00
|
|
|
type RTM struct {
|
2018-12-01 18:55:35 +00:00
|
|
|
// Client is the main API, embedded
|
|
|
|
Client
|
|
|
|
|
2018-08-09 22:38:19 +00:00
|
|
|
idGen IDGenerator
|
|
|
|
pingInterval time.Duration
|
|
|
|
pingDeadman *time.Timer
|
2016-09-05 14:34:37 +00:00
|
|
|
|
|
|
|
// Connection life-cycle
|
|
|
|
conn *websocket.Conn
|
|
|
|
IncomingEvents chan RTMEvent
|
|
|
|
outgoingMessages chan OutgoingMessage
|
|
|
|
killChannel chan bool
|
2019-09-07 20:46:58 +00:00
|
|
|
disconnected chan struct{}
|
|
|
|
disconnectedm *sync.Once
|
2016-09-05 14:34:37 +00:00
|
|
|
forcePing chan bool
|
|
|
|
|
|
|
|
// UserDetails upon connection
|
|
|
|
info *Info
|
2017-07-16 12:29:46 +00:00
|
|
|
|
|
|
|
// useRTMStart should be set to true if you want to use
|
|
|
|
// rtm.start to connect to Slack, otherwise it will use
|
|
|
|
// rtm.connect
|
|
|
|
useRTMStart bool
|
2018-08-09 22:38:19 +00:00
|
|
|
|
|
|
|
// dialer is a gorilla/websocket Dialer. If nil, use the default
|
|
|
|
// Dialer.
|
|
|
|
dialer *websocket.Dialer
|
|
|
|
|
|
|
|
// mu is mutex used to prevent RTM connection race conditions
|
|
|
|
mu *sync.Mutex
|
2016-09-05 14:34:37 +00:00
|
|
|
|
2018-12-01 18:55:35 +00:00
|
|
|
// connParams is a map of flags for connection parameters.
|
|
|
|
connParams url.Values
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 20:46:58 +00:00
|
|
|
// signal that we are disconnected by closing the channel.
|
|
|
|
// protect it with a mutex to ensure it only happens once.
|
|
|
|
func (rtm *RTM) disconnect() {
|
|
|
|
rtm.disconnectedm.Do(func() {
|
|
|
|
close(rtm.disconnected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:34:37 +00:00
|
|
|
// Disconnect and wait, blocking until a successful disconnection.
|
|
|
|
func (rtm *RTM) Disconnect() error {
|
2019-09-07 20:46:58 +00:00
|
|
|
// always push into the kill channel when invoked,
|
2018-08-09 22:38:19 +00:00
|
|
|
// this lets the ManagedConnection() function properly clean up.
|
|
|
|
// if the buffer is full then just continue on.
|
|
|
|
select {
|
2019-09-07 20:46:58 +00:00
|
|
|
case rtm.killChannel <- true:
|
|
|
|
return nil
|
|
|
|
case <-rtm.disconnected:
|
|
|
|
return ErrAlreadyDisconnected
|
2018-08-09 22:38:19 +00:00
|
|
|
}
|
2016-09-05 14:34:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetInfo returns the info structure received when calling
|
2019-09-07 20:46:58 +00:00
|
|
|
// "startrtm", holding metadata needed to implement a full
|
|
|
|
// chat client. It will be non-nil after a call to StartRTM().
|
2016-09-05 14:34:37 +00:00
|
|
|
func (rtm *RTM) GetInfo() *Info {
|
|
|
|
return rtm.info
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMessage submits a simple message through the websocket. For
|
|
|
|
// more complicated messages, use `rtm.PostMessage` with a complete
|
|
|
|
// struct describing your attachments and all.
|
|
|
|
func (rtm *RTM) SendMessage(msg *OutgoingMessage) {
|
|
|
|
if msg == nil {
|
|
|
|
rtm.Debugln("Error: Attempted to SendMessage(nil)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rtm.outgoingMessages <- *msg
|
|
|
|
}
|
2018-08-09 22:38:19 +00:00
|
|
|
|
|
|
|
func (rtm *RTM) resetDeadman() {
|
2019-01-31 16:06:36 +00:00
|
|
|
rtm.pingDeadman.Reset(deadmanDuration(rtm.pingInterval))
|
2018-08-09 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deadmanDuration(d time.Duration) time.Duration {
|
|
|
|
return d * 4
|
|
|
|
}
|