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


Golang Conn.Config方法代碼示例

本文整理匯總了Golang中golang.org/x/net/websocket.Conn.Config方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Config方法的具體用法?Golang Conn.Config怎麽用?Golang Conn.Config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在golang.org/x/net/websocket.Conn的用法示例。


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

示例1: handle

// handle implements a WebSocket handler.
func (r *Reader) handle(ws *websocket.Conn) {
	encode := len(ws.Config().Protocol) > 0 && ws.Config().Protocol[0] == base64BinaryWebSocketProtocol
	defer close(r.err)
	defer ws.Close()
	go IgnoreReceives(ws, r.timeout)
	r.err <- messageCopy(ws, r.r, encode, r.ping, r.timeout)
}
開發者ID:Clarifai,項目名稱:kubernetes,代碼行數:8,代碼來源:stream.go

示例2: initialize

func (conn *Conn) initialize(ws *websocket.Conn) {
	negotiated := ws.Config().Protocol
	conn.selectedProtocol = negotiated[0]
	p := conn.protocols[conn.selectedProtocol]
	if p.Binary {
		conn.codec = rawCodec
	} else {
		conn.codec = base64Codec
	}
	conn.ws = ws
	conn.channels = make([]*websocketChannel, len(p.Channels))
	for i, t := range p.Channels {
		switch t {
		case ReadChannel:
			conn.channels[i] = newWebsocketChannel(conn, byte(i), true, false)
		case WriteChannel:
			conn.channels[i] = newWebsocketChannel(conn, byte(i), false, true)
		case ReadWriteChannel:
			conn.channels[i] = newWebsocketChannel(conn, byte(i), true, true)
		case IgnoreChannel:
			conn.channels[i] = newWebsocketChannel(conn, byte(i), false, false)
		}
	}

	close(conn.ready)
}
開發者ID:kubernetes,項目名稱:kubernetes,代碼行數:26,代碼來源:conn.go

示例3: vncWsHandler

func vncWsHandler(ws *websocket.Conn) {
	// URL should be of the form `/ws/<vm_name>`
	path := strings.Trim(ws.Config().Location.Path, "/")

	fields := strings.Split(path, "/")
	if len(fields) != 2 {
		return
	}
	vmName := fields[1]

	vms := GlobalVMs()
	vm, err := vms.findKvmVM(vmName)
	if err != nil {
		log.Errorln(err)
		return
	}

	// Undocumented "feature" of websocket -- need to set to PayloadType in
	// order for a direct io.Copy to work.
	ws.PayloadType = websocket.BinaryFrame

	// connect to the remote host
	rhost := fmt.Sprintf("%v:%v", vm.Host, vm.VNCPort)
	remote, err := net.Dial("tcp", rhost)
	if err != nil {
		log.Errorln(err)
		return
	}
	defer remote.Close()

	go io.Copy(ws, remote)
	io.Copy(remote, ws)

	log.Info("ws client disconnected from %v", rhost)
}
開發者ID:cdshann,項目名稱:minimega,代碼行數:35,代碼來源:tunnel.go

示例4: handle

// handle implements a WebSocket handler.
func (r *Reader) handle(ws *websocket.Conn) {
	// Close the connection when the client requests it, or when we finish streaming, whichever happens first
	closeConnOnce := &sync.Once{}
	closeConn := func() {
		closeConnOnce.Do(func() {
			ws.Close()
		})
	}

	negotiated := ws.Config().Protocol
	r.selectedProtocol = negotiated[0]
	defer close(r.err)
	defer closeConn()

	go func() {
		defer runtime.HandleCrash()
		// This blocks until the connection is closed.
		// Client should not send anything.
		IgnoreReceives(ws, r.timeout)
		// Once the client closes, we should also close
		closeConn()
	}()

	r.err <- messageCopy(ws, r.r, !r.protocols[r.selectedProtocol].Binary, r.ping, r.timeout)
}
開發者ID:astropuffin,項目名稱:kubernetes,代碼行數:26,代碼來源:stream.go

示例5: handleStatistic

func handleStatistic(ws *websocket.Conn) {
	for k, v := range ws.Config().Header {
		log.Debug.Println(k, " - ", v)
	}

	ws.Write([]byte("Hi"))
}
開發者ID:Avialeta,項目名稱:API,代碼行數:7,代碼來源:statistic.go

示例6: WSCompileRunHandler

// WSCompileRunHandler handles the websocket connection for a given compile/run action.
// It handles transcoding Messages to and from JSON format, and handles starting
// and killing processes.
func WSCompileRunHandler(c *websocket.Conn) {
	c.Config()
	in, out := make(chan *Message), make(chan *Message)
	errc := make(chan error, 1)

	// Decode messages from client and send to the in channel.
	go func() {
		dec := json.NewDecoder(c)
		for {
			var m Message
			if err := dec.Decode(&m); err != nil {
				fmt.Printf("error in dec.Decode %s\n", err)
				errc <- err
				return
			}
			in <- &m
		}
	}()

	// Receive messages from the out channel and encode to the client.
	go func() {
		enc := json.NewEncoder(c)
		for m := range out {
			if err := enc.Encode(m); err != nil {
				fmt.Printf("error in enc.Encode %s\n", err)
				errc <- err
				return
			}
		}
	}()

	// Start and kill Processes and handle errors.
	proc := make(map[string]*Process)
	for {
		select {
		case m := <-in:
			switch m.Kind {
			case "run":
				goTest := m.GoTest
				proc[m.Id].Kill()
				if goTest {
					proc[m.Id] = StartTest(m.Id, m.SrcDir, m.RunOpts, nil, out)
				} else {
					proc[m.Id] = StartProcess(m.Id, m.Body, m.BuildOpts, m.RunOpts, nil, out)
				}
			case "kill":
				proc[m.Id].Kill()
			}
		case err := <-errc:
			// A encode or decode has failed; bail.
			log.Println(err)
			// Shut down any running processes.
			for _, p := range proc {
				p.Kill()
			}
			return
		}
	}
}
開發者ID:wrouesnel,項目名稱:go-play,代碼行數:62,代碼來源:goplay.go

示例7: initialize

func (conn *Conn) initialize(ws *websocket.Conn) {
	protocols := ws.Config().Protocol
	switch {
	case len(protocols) == 0, protocols[0] == channelWebSocketProtocol:
		conn.codec = rawCodec
	case protocols[0] == base64ChannelWebSocketProtocol:
		conn.codec = base64Codec
	}
	conn.ws = ws
	close(conn.ready)
}
開發者ID:johndmulhausen,項目名稱:kubernetes,代碼行數:11,代碼來源:conn.go

示例8: jsonServer

// jsonServer echoes back json string sent from client using websocket.JSON.
func jsonServer(ws *websocket.Conn) {
	fmt.Printf("jsonServer %#v\n", ws.Config())
	for {
		var msg T
		// Receive receives a text message serialized T as JSON.
		err := websocket.JSON.Receive(ws, &msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("recv:%#v\n", msg)
		// Send send a text message serialized T as JSON.
		err = websocket.JSON.Send(ws, msg)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%#v\n", msg)
	}
}
開發者ID:lkj01010,項目名稱:chesssrv,代碼行數:21,代碼來源:ws_sample.go

示例9: readWriteServer

// readWriteServer echoes back messages sent from client using Read and Write.
func readWriteServer(ws *websocket.Conn) {
	fmt.Printf("readWriteServer %#v\n", ws.Config())
	for {
		buf := make([]byte, 100)
		// Read at most 100 bytes.  If client sends a message more than
		// 100 bytes, first Read just reads first 100 bytes.
		// Next Read will read next at most 100 bytes.
		n, err := ws.Read(buf)
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("recv:%q\n", buf[:n])
		// Write send a message to the client.
		n, err = ws.Write(buf[:n])
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf("send:%q\n", buf[:n])
	}
	fmt.Println("readWriteServer finished")
}
開發者ID:lkj01010,項目名稱:chesssrv,代碼行數:24,代碼來源:ws_sample.go

示例10: handle

// handle implements a WebSocket handler.
func (r *Reader) handle(ws *websocket.Conn) {
	// Close the connection when the client requests it, or when we finish streaming, whichever happens first
	closeConnOnce := &sync.Once{}
	closeConn := func() {
		closeConnOnce.Do(func() {
			ws.Close()
		})
	}

	encode := len(ws.Config().Protocol) > 0 && ws.Config().Protocol[0] == base64BinaryWebSocketProtocol
	defer close(r.err)
	defer closeConn()

	go func() {
		defer runtime.HandleCrash()
		// This blocks until the connection is closed.
		// Client should not send anything.
		IgnoreReceives(ws, r.timeout)
		// Once the client closes, we should also close
		closeConn()
	}()
	r.err <- messageCopy(ws, r.r, encode, r.ping, r.timeout)
}
開發者ID:ncdc,項目名稱:origin,代碼行數:24,代碼來源:stream.go

示例11: serveWS

func (e *echoHandler) serveWS(conn *websocket.Conn) {
	c := conn.Config()
	r := conn.Request()
	h := map[string]string{}
	for k, _ := range r.Header {
		h[k] = r.Header.Get(k)
	}
	b, _ := json.MarshalIndent(&struct {
		Location string
		Origin   string
		Protocol []string
		Version  int
		Headers  map[string]string
	}{
		Location: c.Location.String(),
		Origin:   c.Origin.String(),
		Protocol: c.Protocol,
		Version:  c.Version,
		Headers:  h,
	}, "", "  ")
	conn.Write(b)
	io.Copy(conn, conn)
	conn.Close()
}
開發者ID:jpillora,項目名稱:go-echo-server,代碼行數:24,代碼來源:handler.go

示例12: copyServer

// copyServer echoes back messages sent from client using io.Copy.
func copyServer(ws *websocket.Conn) {
	fmt.Printf("copyServer %#v\n", ws.Config())
	io.Copy(ws, ws)
	fmt.Println("copyServer finished")
}
開發者ID:lkj01010,項目名稱:chesssrv,代碼行數:6,代碼來源:ws_sample.go


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