Skip to content

Commit

Permalink
Add a constructor for anonymous usage (#124)
Browse files Browse the repository at this point in the history
Fixes #123
  • Loading branch information
pajlada authored Mar 29, 2020
1 parent b5d4670 commit 263d07e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

func main() {
client := twitch.NewClient("justinfan123123", "oauth:123123123")
// or client := twitch.NewAnonymousClient() for an anonymous user (no write capabilities)
client := twitch.NewClient("yourtwitchusername", "oauth:123123123")

client.OnPrivateMessage(func(message twitch.PrivateMessage) {
fmt.Println(message.Message)
Expand Down
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ func NewClient(username, oauth string) *Client {
}
}

// NewAnonymousClient to create a new client without login requirements (anonymous user)
// Do note that the Say and Whisper functions will be ineffectual when using this constructor
func NewAnonymousClient() *Client {
return NewClient("justinfan123123", "oauth:59301")
}

// OnConnect attach callback to when a connection has been established
func (c *Client) OnConnect(callback func()) {
c.onConnect = callback
Expand Down
55 changes: 55 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ func newTestClient(host string) *Client {
return client
}

func newAnonymousTestClient(host string) *Client {
client := NewAnonymousClient()
client.IrcAddress = host

return client
}

func connectAndEnsureGoodDisconnect(t *testing.T, client *Client) chan struct{} {
c := make(chan struct{})

go func() {
err := client.Connect()
assertErrorsEqual(t, ErrClientDisconnected, err)
Expand Down Expand Up @@ -423,6 +431,53 @@ func TestFullConnectAndDisconnect(t *testing.T) {
<-server.stopped
}

func TestCanConnectAndAuthenticateAnonymous(t *testing.T) {
t.Parallel()
const oauthCode = "oauth:59301"
waitPass := make(chan struct{})
waitServerConnect := make(chan struct{})
waitClientConnect := make(chan struct{})

var received string

server := startServer2(t, closeOnConnect(waitServerConnect), closeOnPassReceived(&received, waitPass))

client := newAnonymousTestClient(server.host)
client.OnConnect(clientCloseOnConnect(waitClientConnect))
clientDisconnected := connectAndEnsureGoodDisconnect(t, client)

// Wait for server to acknowledge connection
if !waitWithTimeout(waitServerConnect) {
t.Fatal("no successful connection")
}

// Wait for client to acknowledge connection
if !waitWithTimeout(waitClientConnect) {
t.Fatal("no successful connection")
}

// Wait to receive password
select {
case <-waitPass:
case <-time.After(time.Second * 3):
t.Fatal("no oauth read")
}

assertStringsEqual(t, "PASS "+oauthCode, received)

// Disconnect client from server
err := client.Disconnect()
if err != nil {
t.Error("Error during disconnect:" + err.Error())
}

// Wait for client to be fully disconnected
<-clientDisconnected

// Wait for server to be fully disconnected
<-server.stopped
}

func TestCanDisconnect(t *testing.T) {
t.Parallel()
wait := make(chan struct{})
Expand Down

0 comments on commit 263d07e

Please sign in to comment.