-
Notifications
You must be signed in to change notification settings - Fork 5
/
dggchat.go
44 lines (38 loc) · 1.09 KB
/
dggchat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Package dggchat provides destiny gg chat binding for Go
package dggchat
import (
"errors"
"net/url"
"os"
"github.com/gorilla/websocket"
)
// ErrTooManyArgs is thrown when a funcion is called with an unexpeted number of arguments
var ErrTooManyArgs = errors.New("function called with unexcepted amount of arguments")
// New creates a new destinygg session. Accepts either 0 or 1 arguments.
// If no login key is provided, a read-only session is returned
func New(args ...string) (*Session, error) {
if len(args) > 1 {
return nil, ErrTooManyArgs
}
s := &Session{
attempToReconnect: true,
state: newState(),
dialer: websocket.DefaultDialer,
}
customHost, customHostExist := os.LookupEnv("CUSTOM_WSHOST")
if customHostExist {
s.wsURL = url.URL{Scheme: "wss", Host: customHost, Path: "/ws"}
} else {
s.wsURL = wsURL
}
customOriginHeader, customOriginHeaderExist := os.LookupEnv("CUSTOM_ORIGINHEADER")
if customOriginHeaderExist {
s.originHeader = customOriginHeader
}
if len(args) == 1 {
s.loginKey = args[0]
} else {
s.readOnly = true
}
return s, nil
}