func connect() *connection {
	fmt.Println("Starting connection...")
	dialer := websocket.Dialer{}
	conn, _, err := dialer.Dial(Url, nil)
	if err != nil {
		log.Fatal("Unable to connect to websocket.")
	}
	c := &connection{ws: conn}
	c.setPinger()
	return c
}
Creates and returns connection
package main
import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
	"strings"
	"github.com/gorilla/websocket"
)
const (
	PingInterval = time.Second * 30
	Url          = "wss://ws.blockchain.info:443/inv"
	Usage        = `
	  Usage:
	  	bithook <command> [webhook-url]
		bithook blocks -- Subscribe to new blocks.
		bithook unconfirmed -- Subscribe to new unconfirmed transactions.
		bithook address <address> -- Subscribe to address.
		bithook test -- Receives latest transaction. Use for testing.
		bithook help -- This help menu.
		bithook version -- This version.
	`
	Version = "0.0.2"
)
var webhookFlag string
type connection struct {
	ws   *websocket.Conn
	conn *websocket.Dialer
}
type response struct {
	Data map[string]interface{} `json:"data"`
}
func init() {
	if len(os.Args[1:]) < 1 {
		fmt.Println("Please enter a command.")
		fmt.Println(Usage)
		os.Exit(1)
	}
	hasWebhook := strings.HasPrefix(os.Args[len(os.Args)-1], "http")
	if hasWebhook {
		webhookFlag = os.Args[len(os.Args)-1]
	}
}
func (c *connection) send(messageType int, payload []byte) {
	fmt.Printf("Send: %s\n", payload)
	c.ws.WriteMessage(messageType, payload)
}
func (c *connection) subAddress(addr string) {
	addrStr := fmt.Sprintf(`{"op": "addr_sub", "addr": "%s"}`, addr)
	byt := []byte(addrStr)
	c.send(websocket.TextMessage, byt)
	c.read()
}
func (c *connection) subBlocks() {
	byt := []byte(`{"op": "blocks_sub"}`)
	c.send(websocket.TextMessage, byt)
	c.read()
}
func (c *connection) subUnconfirmed() {
	byt := []byte(`{"op": "unconfirmed_sub"}`)
	c.send(websocket.TextMessage, byt)
	c.read()
}
func (c *connection) debugPing() {
	byt := []byte(`{"op": "ping_tx"}`)
	c.send(websocket.TextMessage, byt)
	c.read()
}
func (c *connection) debugPingBlock() {
	byt := []byte(`{"op": "ping_block"}`)
	c.send(websocket.TextMessage, byt)
	c.read()
}
func (c *connection) read() {
	for {
		var dat map[string]interface{}
		if err := c.ws.ReadJSON(&dat); err != nil {
			log.Fatal(err)
		}
		fmt.Println(dat)
		jsonResponse := &response{Data: dat}
		res, _ := json.Marshal(jsonResponse)
		webHook(res)
	}
}
func (c *connection) setPinger() {
	ticker := time.NewTicker(PingInterval)
	go func() {
		for {
			select {
			case <-ticker.C:
				byt := []byte(`"op": "ping"`)
				c.send(websocket.PingMessage, byt)
			}
		}
	}()
}
func webHook(data []byte) {
	url := webhookFlag
	if url == "" {
		return
	}
	fmt.Println("Sending Request: ", url)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
	ua := fmt.Sprintf("bithook-client-%s", Version)
	req.Header.Set("User-Agent", ua)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("response Status:", resp.Status)
	fmt.Println("response Headers:", resp.Header)
	fmt.Println("response Body:", string(body))
}