Skip to content

Commit

Permalink
Added 'headers' parameter for pass custom headers to websocket connec…
Browse files Browse the repository at this point in the history
…tion (#4)
  • Loading branch information
Tolsi authored and lafikl committed Oct 9, 2016
1 parent 02d84ac commit 1d8ea30
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ import (
)

var url = flag.String("url", "", "-url ws://127.0.0.1")
var headers = flag.String("headers", "", "-headers name1=value1,name2=value2")

func main() {
flag.Parse()
if *url == "" {
log.Fatal("URL can't be empty.")
}
headersMap := make(map[string]string)
if *headers != "" {
headersSeq := strings.Split(*headers, ",")
for _, headerString := range headersSeq {
splittedHeader := strings.Split(headerString, "=")
headersMap[splittedHeader[0]] = splittedHeader[1]
}
}
done := make(chan bool)
c := NewClient(*url)
c := NewClient(*url, headersMap)

go func() {
for {
Expand Down Expand Up @@ -52,9 +61,12 @@ func main() {

}

func NewClient(url string) *websocket.Conn {
func NewClient(url string, headers map[string]string) *websocket.Conn {
r, _ := http.NewRequest("GET", url, nil)
r.Header.Add("Content-Type", "application/json")
for key, value := range headers {
r.Header.Add(key, value)
}
c, _, err := websocket.DefaultDialer.Dial(url, r.Header)
if err != nil {
log.Fatal("errrr ", err)
Expand Down

0 comments on commit 1d8ea30

Please sign in to comment.