當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。