当前位置: 首页>>代码示例>>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;未经允许,请勿转载。