예제 #1
0
func connect(rpc *duplex.RPC, path string, backend bool) (*duplex.Peer, error) {
	var baseUrl, token, secret, url string
	if os.Getenv("HUB_URL") != "" {
		baseUrl = os.Getenv("HUB_URL")
	} else {
		baseUrl = "ws://localhost:8080"
	}
	if os.Getenv("SECRET") != "" {
		secret = os.Getenv("SECRET")
	} else {
		secret = "test"
	}
	if backend {
		if os.Getenv("TOKEN") != "" {
			token = os.Getenv("TOKEN")
		} else {
			token = "dev"
		}
		url = fmt.Sprintf("%s%s?token=%s&secret=%s",
			baseUrl, path, token, secret)
	} else {
		url = fmt.Sprintf("%s%s?secret=%s",
			baseUrl, path, secret)
	}
	ws, err := websocket.Dial(url, "", "http://example.com")
	if err != nil {
		return nil, err
	}
	return rpc.Handshake(ws)
}
예제 #2
0
// This example demonstrates a trivial client.
func ExampleDial() {
	origin := "http://localhost/"
	url := "ws://localhost:12345/ws"
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		log.Fatal(err)
	}
	if _, err := ws.Write([]byte("hello, world!\n")); err != nil {
		log.Fatal(err)
	}
	var msg = make([]byte, 512)
	var n int
	if n, err = ws.Read(msg); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Received: %s.\n", msg[:n])
}