當前位置: 首頁>>代碼示例>>Golang>>正文


Golang websocket.NewClient函數代碼示例

本文整理匯總了Golang中github.com/gorilla/websocket.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: newClientConnection

func newClientConnection(connectionUrl string) (conn *websocket.Conn) {
	u, err := url.Parse(connectionUrl)
	if err != nil {
		Error.Println(err)
	}

	Trace.Printf("url for new client connection is %+v\n", u)

	Info.Println("Attempting to dial " + u.Host)

	rawConn, err := net.Dial("tcp", u.Host)
	if err != nil {
		Error.Printf("Failed to dial %s. Error: %+v\n", err)
		return nil
	}

	wsHeaders := http.Header{
		"Origin": {u.Host},
		// your milage may differ
		"Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"},
	}

	wsConn, resp, err := websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024)

	if err != nil {
		Error.Printf("websocket.NewClient Error: %s\nResp:%+v", err, resp)
	}

	Info.Printf("Successfully connected to %+v", wsConn.RemoteAddr())

	return wsConn
}
開發者ID:etinlb,項目名稱:go_server_experiment,代碼行數:32,代碼來源:server_handlers.go

示例2: openSocketToContainer

func (c *ContainerConnection) openSocketToContainer() error {
	id := c.id
	endpoint := GlobalConfig.GetDockerEndpoint()
	u, err := url.Parse(endpoint + "/v" + DockerApiVersion + "/containers/" + id + "/attach/ws?logs=0&stderr=1&stdout=1&stream=1&stdin=1")
	if err != nil {
		return err
	}

	rawConn, err := net.Dial("tcp", u.Host)
	if err != nil {
		return err
	}

	wsHeaders := http.Header{
		"Origin": {"http://localhost:4500"},
	}

	wsConn, resp, err := websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024)

	if err != nil {
		Log.Error("Unable to connect", resp, err)
	}
	c.container = wsConn
	return err
}
開發者ID:nishizhen,項目名稱:codetainer,代碼行數:25,代碼來源:container.go

示例3: NewWebsocket

func (b BackendController) NewWebsocket(connectionUrl string) (*websocket.Conn, error) {
	u, err := url.Parse(connectionUrl)
	if err != nil {
		log.Println(err)
		return nil, errors.New("Cannot parse connection url" + connectionUrl)
	}
	log.Println(u)

	log.Println(u.Host)
	rawConn, err := net.Dial("tcp", u.Host)
	if err != nil {
		log.Println(err)
		return nil, errors.New("cannot dial " + u.Host)
	}

	wsHeaders := http.Header{
		"Origin": {u.Host},
		// your milage may differ
		"Sec-WebSocket-Extensions": {
			"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"},
	}

	wsConn, resp, err := websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024)

	if err != nil {

		return nil, fmt.Errorf("websocket.NewClient Error: %s\nResp:%+v", err, resp)

	}

	b.AddNewConnection(wsConn)

	return wsConn, nil
}
開發者ID:etinlb,項目名稱:go_server_experiment,代碼行數:34,代碼來源:main.go

示例4: NewClient

func NewClient(originURL, targetURL string, settings map[string]interface{}) (c *websocket.Conn, response *http.Response, err error) {
	u, err := url.Parse(targetURL)
	if err != nil {
		return nil, nil, err
	}

	dialer := net.Dialer{}

	timeoutInterface, ok := settings["Timeout"]
	if ok {
		dialer.Timeout = timeoutInterface.(time.Duration)
	}

	keepAliveInterface, ok := settings["KeepAlive"]
	if ok {
		dialer.KeepAlive = keepAliveInterface.(time.Duration)
	}

	rawConn, err := dialer.Dial("tcp", u.Host)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"Error": err.Error(),
		}).Errorf("TCP connection error when dialing %v", u.Host)

		return nil, nil, err
	}

	wsHeaders := http.Header{
		"Origin": {originURL},
		// your milage may differ
		"Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"},
	}

	return websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024)
}
開發者ID:actaeon,項目名稱:resourced,代碼行數:35,代碼來源:wstrafficker.go

示例5: connect

func (wi *websocketInstance) connect() {
	//keep trying to get a connection

	var conn net.Conn
	var dialConnection func()
	dialConnection = func() {
		_conn, err := net.Dial("tcp", wi.address+":"+wi.port)
		if err != nil {
			defer dialConnection()
			time.Sleep(pingPeriod)
			return
		}
		conn = _conn
	}
	dialConnection()

	log.Println("connection found")

	site, err := url.Parse("ws://" + wi.address + ":" + wi.port + wi.path)

	wi.socket, _, err = websocket.NewClient(conn, site, nil, bufferSize, bufferSize)

	if err != nil {
		if _, ok := err.(websocket.HandshakeError); !ok {
			log.Println(err)
		}
		return
	}

	go wi.writeHandler()
	go wi.readHandler()
}
開發者ID:dazzaji,項目名稱:Flare,代碼行數:32,代碼來源:websockets.go

示例6: connectOnce

func (ws *WSConnection) connectOnce(r *Room, try int) error {
	r.Logger.Infof("Connecting to room %s...", r.RoomName)
	tlsConn, err := tls.Dial("tcp", "euphoria.io:443", &tls.Config{})
	if err != nil {
		return err
	}
	roomURL, err := url.Parse(fmt.Sprintf("wss://euphoria.io/room/%s/ws", r.RoomName))
	if err != nil {
		return err
	}
	wsConn, _, err := websocket.NewClient(tlsConn, roomURL, http.Header{}, 4096, 4096)
	if err != nil {
		return err
	}
	if err := r.Ctx.Check("connectOnce", try); err != nil {
		return err
	}
	ws.conn = wsConn
	if r.password != "" {
		if _, err := r.sendAuth(); err != nil {
			return err
		}
	}
	if _, err := r.sendNick(); err != nil {
		return err
	}
	return nil
}
開發者ID:CylonicRaider,項目名稱:gobot,代碼行數:28,代碼來源:connection.go

示例7: Connect

func (c *GremlinClient) Connect() {
	host := c.Addr + ":" + strconv.FormatInt(int64(c.Port), 10)

	conn, err := net.Dial("tcp", host)
	if err != nil {
		logging.GetLogger().Error("Connection to the WebSocket server failed: %s", err.Error())
		return
	}

	endpoint := "ws://" + host
	u, err := url.Parse(endpoint)
	if err != nil {
		logging.GetLogger().Error("Unable to parse the WebSocket Endpoint %s: %s", endpoint, err.Error())
		return
	}

	wsConn, _, err := websocket.NewClient(conn, u, http.Header{}, 0, 4096)
	if err != nil {
		logging.GetLogger().Error("Unable to create a WebSocket connection %s : %s", endpoint, err.Error())
		return
	}

	logging.GetLogger().Info("Connected to gremlin server %s:%d", c.Addr, c.Port)

	c.wsConn = wsConn
}
開發者ID:mestery,項目名稱:skydive,代碼行數:26,代碼來源:client.go

示例8: TestWebsocketQuery

func TestWebsocketQuery(t *testing.T) {
	w := test.GetWorld(t)
	pn := w.NewPermanode(t)
	test.MustRunCmd(t, w.Cmd("camput", "attr", pn.String(), "tag", "foo"))

	check := func(err error) {
		if err != nil {
			t.Fatal(err)
		}
	}

	const bufSize = 1 << 20

	c, err := net.Dial("tcp", w.Addr())
	if err != nil {
		t.Fatalf("Dial: %v", err)
	}
	defer c.Close()

	wc, _, err := websocket.NewClient(c, &url.URL{Host: w.Addr(), Path: w.SearchHandlerPath() + "ws"}, nil, bufSize, bufSize)
	check(err)

	msg, err := wc.NextWriter(websocket.TextMessage)
	check(err)

	_, err = msg.Write([]byte(`{"tag": "foo", "query": { "expression": "tag:foo" }}`))
	check(err)
	check(msg.Close())

	errc := make(chan error, 1)
	go func() {
		inType, inMsg, err := wc.ReadMessage()
		if err != nil {
			errc <- err
			return
		}
		if !strings.HasPrefix(string(inMsg), `{"tag":"_status"`) {
			errc <- fmt.Errorf("unexpected message type=%d msg=%q, wanted status update", inType, inMsg)
			return
		}
		inType, inMsg, err = wc.ReadMessage()
		if err != nil {
			errc <- err
			return
		}
		if strings.Contains(string(inMsg), pn.String()) {
			errc <- nil
			return
		}
		errc <- fmt.Errorf("unexpected message type=%d msg=%q", inType, inMsg)
	}()
	select {
	case err := <-errc:
		if err != nil {
			t.Error(err)
		}
	case <-time.After(5 * time.Second):
		t.Error("timeout")
	}
}
開發者ID:camlistore,項目名稱:camlistore,代碼行數:60,代碼來源:camlistore_test.go

示例9: NewConnection

func NewConnection(pin string) (*Connection, error) {
	gameId, err := strconv.Atoi(pin)
	if err != nil {
		return nil, err
	}

	conn, err := tls.Dial("tcp", "kahoot.it:443", nil)
	if err != nil {
		return nil, err
	}

	url, _ := url.Parse("wss://kahoot.it/cometd")
	reqHeader := http.Header{}
	reqHeader.Set("Origin", "https://kahoot.it")
	reqHeader.Set("Cookie", "no.mobitroll.session="+pin)
	ws, _, err := websocket.NewClient(conn, url, reqHeader, 100, 100)
	if err != nil {
		return nil, err
	}

	incoming := make(chan incomingData, 100)
	res := &Connection{ws, 0, gameId, "", incoming}
	go res.readLoop()
	return res, nil
}
開發者ID:nick2228,項目名稱:kahoot-hack,代碼行數:25,代碼來源:connection.go

示例10: openConnection

// Open WebSocket connection to Kubernetes API server
func openConnection(endpoint string) *websocket.Conn {
	u, err := url.Parse(endpoint)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Connect to K8S master host: %v", u.Host)
	rawConn, err := net.Dial("tcp", u.Host)
	if err != nil {
		log.Fatal(err)
	}

	wsHeaders := http.Header{
		"Origin":                   {endpoint},
		"Sec-WebSocket-Extensions": {"permessage-deflate; client_max_window_bits, x-webkit-deflate-frame"},
	}

	wsConn, resp, err := websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024)
	if err != nil {
		log.Fatalf("websocket.NewClient Error: %s\nResp:%+v", err, resp)

	}

	return wsConn
}
開發者ID:rainbean,項目名稱:Kube2Vulcan,代碼行數:26,代碼來源:kube2vulcan.go

示例11: openSocketToContainer

func (c *ContainerConnection) openSocketToContainer() error {
	id := c.id
	endpoint := GlobalConfig.GetDockerEndpoint()
	u, err := url.Parse(endpoint + "/v" + DockerApiVersion + "/containers/" + id + "/attach/ws?logs=0&stderr=1&stdout=1&stream=1&stdin=1")
	if err != nil {
		return err
	}

	var rawConn net.Conn
	if GlobalConfig.DockerServerUseHttps {
		rawConn, err = tls.Dial("tcp", u.Host, GlobalConfig.tlsConfig)
	} else {
		rawConn, err = net.Dial("tcp", u.Host)
	}
	if err != nil {
		return err
	}

	wsHeaders := http.Header{
		"Origin": {endpoint},
	}

	wsConn, resp, err := websocket.NewClient(rawConn, u, wsHeaders, 1024, 1024)

	if err != nil {
		Log.Error("Unable to connect", resp, err)
	}
	c.container = wsConn
	return err
}
開發者ID:mslsoftware,項目名稱:codetainer,代碼行數:30,代碼來源:container.go

示例12: wsClient

func wsClient(conn net.Conn, host string) (*wsConn, error) {
	c, resp, err := websocket.NewClient(conn, &url.URL{Scheme: "ws", Host: host, Path: "/ws"}, nil, 4096, 4096)
	if err != nil {
		return nil, err
	}
	resp.Body.Close()

	return &wsConn{conn: c}, nil
}
開發者ID:jim1568cas,項目名稱:gost,代碼行數:9,代碼來源:ws.go

示例13: connectWebsocketNoHandshake

func connectWebsocketNoHandshake(t *testing.T, server *httptest.Server, header http.Header) (*websocket.Conn, error) {
	conn := connectToTestServer(t, server)
	url := urlMustParse(server.URL)
	url.Path = "/ctrl"
	url.Scheme = "ws"
	ws, _, err := websocket.NewClient(conn, url, header, 1024, 1024)
	if err != nil {
		return nil, fmt.Errorf("raw websocket client: %v", err)
	}
	return ws, nil
}
開發者ID:hraban,項目名稱:lush,代碼行數:11,代碼來源:websocket_test.go

示例14: connectWebsocket

func connectWebsocket(server *httptest.Server, path string) (*websocket.Conn, error) {
	conn, err := connectToTestServer(server)
	if err != nil {
		return nil, err
	}
	url := urlMustParse(server.URL + path)
	ws, _, err := websocket.NewClient(conn, url, nil, 1024, 1024)
	if err != nil {
		return nil, err
	}
	return ws, nil
}
開發者ID:hraban,項目名稱:websockethub,代碼行數:12,代碼來源:websocket_test.go

示例15: newClient

func newClient() (*websocket.Conn, error) {
	u, _ := url.Parse(WSURL)
	conn, err := net.Dial("tcp", ADDR)

	if err != nil {
		return nil, err
	}

	header := http.Header{"Origin": {ORIGIN}}
	ws, _, err := websocket.NewClient(conn, u, header, 1024, 1024)
	return ws, err
}
開發者ID:russellwmy,項目名稱:goddp,代碼行數:12,代碼來源:websocket_test.go


注:本文中的github.com/gorilla/websocket.NewClient函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。