当前位置: 首页>>代码示例>>Golang>>正文


Golang Conn.LocalAddr方法代码示例

本文整理汇总了Golang中golang.org/x/net/websocket.Conn.LocalAddr方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.LocalAddr方法的具体用法?Golang Conn.LocalAddr怎么用?Golang Conn.LocalAddr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在golang.org/x/net/websocket.Conn的用法示例。


在下文中一共展示了Conn.LocalAddr方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handler

func (l *listener) handler(ws *websocket.Conn) {
	l.lock.Lock()

	if !l.running {
		ws.Close()
		l.lock.Unlock()
		return
	}

	w := &wsPipe{ws: ws, addr: l.addr, proto: l.proto, open: true}
	w.iswss = l.iswss

	req := ws.Request()

	w.props = make(map[string]interface{})
	w.props[mangos.PropLocalAddr] = ws.LocalAddr()
	w.props[mangos.PropRemoteAddr], _ = net.ResolveTCPAddr("tcp", req.RemoteAddr)

	if l.iswss {
		w.props[mangos.PropTlsConnState] = *req.TLS
	}

	w.wg.Add(1)
	l.pending = append(l.pending, w)
	l.cv.Broadcast()
	l.lock.Unlock()

	// We must not return before the socket is closed, because
	// our caller will close the websocket on our return.
	w.wg.Wait()
}
开发者ID:rlhatcher,项目名称:mangos,代码行数:31,代码来源:ws.go

示例2: serveWebsocket

func serveWebsocket(conn *websocket.Conn) {
	var (
		// ip addr
		lAddr = conn.LocalAddr()
		rAddr = conn.RemoteAddr()
		// timer
		tr = DefaultServer.round.Timer(rand.Int())
	)
	log.Debug("start websocket serve \"%s\" with \"%s\"", lAddr, rAddr)
	DefaultServer.serveWebsocket(conn, tr)
}
开发者ID:huweixuan,项目名称:goim,代码行数:11,代码来源:websocket.go

示例3: EchoServer

func EchoServer(ws *websocket.Conn) {
	addr := ws.LocalAddr().String()
	pfx := []byte("[" + addr + "] ")

	log.Printf("ws connect on %s", addr)

	// the following could be done with io.Copy(ws, ws)
	// but I want to add some meta data
	var msg = make([]byte, 1024)
	for {
		n, err := ws.Read(msg)
		if err != nil && err != io.EOF {
			log.Printf("ws error on %s. %s", addr, err)
			break
		}
		_, err = ws.Write(append(pfx, msg[:n]...))
		if err != nil && err != io.EOF {
			log.Printf("ws error on %s. %s", addr, err)
			break
		}
	}
	log.Printf("ws disconnect on %s", addr)
}
开发者ID:hilerchyn,项目名称:fabio,代码行数:23,代码来源:server.go

示例4: wsHandler

func wsHandler(ws *websocket.Conn) {
	done := make(chan bool)

	raw := make([]byte, 1024)
	n, err := ws.Read(raw)
	if err != nil {
		log(err)
		return
	}

	var address string
	err = json.Unmarshal(raw[:n], &address)
	if err != nil {
		log(err, string(raw[:n]))
	}

	registerSocket <- socket{ws, address, done, ws.LocalAddr().String()}

	go incomingMessageReader(ws)

	// when this function finishes the socket will be closed
	<-done
	ws.Close()
}
开发者ID:nathankerr,项目名称:seed,代码行数:24,代码来源:wsjson.go

示例5: GWMockServer

func GWMockServer(ws *websocket.Conn) {
	d, _ := strconv.Atoi(os.Args[1])
	DELAY := time.Duration(d)
	logger := new(Logger)
	logger.Print("Connection from %s", ws.LocalAddr().String())
	elements := make([]string, 0)
	counter := 0
	var request interface{}
	var response *Response

	for {
		// Receive request
		err := websocket.JSON.Receive(ws, &request)
		if err != nil {
			break // EOF
		}
		req := request.(map[string]interface{})
		logger.SetPrefix("* ")
		m, _ := json.Marshal(request)
		logger.Print("Received message: %s", m)

		// Prepare response
		response = nil
		logger.SetPrefix("* .. ")
		switch req["command"] {
		case ADDVERTEX:
			// Generate id
			id := "v_" + randId()
			// Add element to array
			elements = append(elements, id)
			// Return element ID
			response = &Response{
				Requestid: req["requestId"].(string),
				Success:   true,
				Body:      map[string]string{"id": id},
			}
		case CHANGEVERTEX:
			// In the case of changing element name, check if
			// it starts with `v_`.
			props := req["properties"].(map[string]interface{})
			if props["name"] == nil || strings.HasPrefix(props["name"].(string), "v_") {
				response = &Response{
					Requestid: req["requestId"].(string),
					Success:   true,
				}
			} else {
				response = &Response{
					Requestid: req["requestId"].(string),
					Success:   false,
					Body:      map[string]string{"error": "Bad name!"},
				}
			}
		case ADDEDGE:
			// Generate id
			id := "e_" + randId()
			// Add element to array
			elements = append(elements, id)
			// Return element ID
			response = &Response{
				Requestid: req["requestId"].(string),
				Success:   true,
				Body:      map[string]string{"id": id},
			}
		case CHANGEEDGE:
			// In the case of changing element name, check if
			// it starts with `v_`.
			props := req["properties"].(map[string]interface{})
			if props["name"] == nil || strings.HasPrefix(props["name"].(string), "e_") {
				response = &Response{
					Requestid: req["requestId"].(string),
					Success:   true,
				}
			} else {
				response = &Response{
					Requestid: req["requestId"].(string),
					Success:   false,
					Body:      map[string]string{"error": "Bad name!"},
				}
			}
		case NEXT:
			if counter == len(elements)*3 || counter == -1 {
				response = &Response{
					Requestid: req["requestId"].(string),
					Success:   true,
					Body:      map[string]string{"message": "Finished running"},
				}
			} else {
				element := elements[rand.Intn(len(elements))]
				var elementType string
				switch element[0:1] {
				case "v":
					elementType = "vertex"
				case "e":
					elementType = "edge"
				}
				response = &Response{
					Requestid: req["requestId"].(string),
					Success:   true,
					// Return just a random element
					Body: map[string]string{
//.........这里部分代码省略.........
开发者ID:miloszw,项目名称:GraphWalkerStudio,代码行数:101,代码来源:gwmock.go

示例6: EchoServer

// Echo the data received on the WebSocket.
func EchoServer(ws *websocket.Conn) {
	totle++
	fmt.Println(totle, ws.LocalAddr())
	io.Copy(ws, ws)
}
开发者ID:lomoalbert,项目名称:wshub,代码行数:6,代码来源:service.go


注:本文中的golang.org/x/net/websocket.Conn.LocalAddr方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。