Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to get response header in client.go #524

Closed
fbens opened this issue Jul 18, 2019 · 3 comments
Closed

[question] How to get response header in client.go #524

fbens opened this issue Jul 18, 2019 · 3 comments
Labels

Comments

@fbens
Copy link

fbens commented Jul 18, 2019

Describe the problem you're having

A clear and concise description of what the bug is.
How can I get the response header in client.go

Versions

Go version: go version
go version go1.12.7 windows/amd64

"Show me the code!"

A minimal code snippet can be useful, otherwise we're left guessing!

// client.go
package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"os/signal"
	"encoding/json"

	"github.com/gorilla/websocket"
)

var (
	addr = flag.String("addr", "127.0.0.1:17505", "http service address")
	path = flag.String("path", "/ws", "ws path")
	auth = flag.String("auth", "","auth key")
	data = flag.String("data", "ping","put data,eg: ping")
)

func usage() {
	fmt.Printf("Usage Example: %s -data 'ping'\n", os.Args[0])
	fmt.Printf("Usage Example: %s -data '%s'\n", os.Args[0],
		`{reqid:"10000",eid:"11111",action:"on",uptime:"1111111"}`)
	flag.PrintDefaults()
}

func init(){
	flag.Usage = usage
}

func main() {
	flag.Parse()

	log.SetFlags(0)

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)

	u := url.URL{Scheme: "ws", Host: *addr, Path: *path}
	log.Printf("connecting to %s", u.String())
	
	h := http.Header{"X-Websocket-Key": []string{*auth} }
	c, _, err := websocket.DefaultDialer.Dial(u.String(), h)
	if err != nil {
		log.Fatal("dial:", err)
	}
	defer c.Close()

	// print request header
	h2, _ := json.Marshal(h)
	fmt.Printf("request header: %s\n",h2)

	done := make(chan struct{})
	

	go func() {
		defer close(done)
		for {
			_, message, err := c.ReadMessage()
			if err != nil {
				log.Println("read:", err)
				return
			}
			log.Printf("recv: %s", message)
		}
	}()

	err = c.WriteMessage(websocket.TextMessage, []byte(*data))
	if err != nil {
		log.Println("write:", err)
		return
	}
	fmt.Println("send: ",*data)

	for {
		select {
		case <-done:
			return
		case <-interrupt:
			log.Println("interrupt")
			err := c.WriteMessage(websocket.CloseMessage, 
				websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
			if err != nil {
				log.Println("write close:", err)
				return
			}
			return
		}
	}
}
@fbens fbens added the question label Jul 18, 2019
@stephenyama
Copy link

stephenyama commented Jul 18, 2019

c, resp, err := websocket.DefaultDialer.Dial(u.String(), h)
header := resp.Header

@fbens
Copy link
Author

fbens commented Jul 18, 2019

c, resp, err := websocket.DefaultDialer.Dial(u.String(), h)
header := resp.Header

Cool, Thank you very much. :-)

@elithrar
Copy link
Contributor

Closing as resolved, but let us know if any more questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants