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


Golang Socket.Recv方法代碼示例

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


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

示例1: client

func client(url string, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = sub.NewSocket(); err != nil {
		die("can't get new sub socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on sub socket: %s", err.Error())
	}
	// Empty byte array effectively subscribes to everything
	err = sock.SetOption(mangos.OptionSubscribe, []byte(""))
	if err != nil {
		die("cannot subscribe: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("CLIENT(%s): RECEIVED %s\n", name, string(msg))
	}
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:25,代碼來源:pubsub.go

示例2: StartNanomsgJsonServer

func StartNanomsgJsonServer(port string) {
	var server mangos.Socket
	var err error

	if server, err = rep.NewSocket(); err != nil {
		log.Fatal(err)
	}

	server.AddTransport(tcp.NewTransport())
	server.Listen("tcp://0.0.0.0:" + port)

	for {
		msg, _ := server.Recv()

		var body Request
		var response []byte

		json.Unmarshal(msg, &body)

		if response, err = json.Marshal(Response{Method: body.Method, PayloadLength: len(body.Payload)}); err != nil {
			log.Fatal(err)
		}

		server.Send(response)
	}
}
開發者ID:sguzwf,項目名稱:internal-api-benchmark,代碼行數:26,代碼來源:nanomsg-json.go

示例3: recv_name

func recv_name(sock mangos.Socket, name string) {
	var msg []byte
	var err error
	if msg, err = sock.Recv(); err == nil {
		fmt.Printf("%s: RECEIVED: \"%s\"\n", name, string(msg))
	}
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:7,代碼來源:pair.go

示例4: sendRequest

func sendRequest(s mangos.Socket, request request) (*response, error) {
	fmt.Println("Sending a request")

	requestJSON, err := json.Marshal(request)
	if err != nil {
		// This is not recoverable.
		panic(err)
	}

	fmt.Printf("sendRequest: %s\n", requestJSON)

	if err := s.Send(requestJSON); err != nil {
		return nil, err
	}

	rep, err := s.Recv()
	if err != nil {
		return nil, err
	}

	var resp response
	if err := json.Unmarshal(rep, &resp); err != nil {
		return nil, err
	}

	return &resp, nil
}
開發者ID:bunin,項目名稱:Flotilla,代碼行數:27,代碼來源:client.go

示例5: StartNanomsgJsonTest

func StartNanomsgJsonTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) {
	return func(wg *sync.WaitGroup) {
		var socket mangos.Socket
		var err error

		if socket, err = req.NewSocket(); err != nil {
			log.Fatal(err)
		}

		defer socket.Close()
		defer wg.Done()

		socket.AddTransport(tcp.NewTransport())

		if err = socket.Dial("tcp://" + address); err != nil {
			log.Fatal(err)
		}

		request, _ := json.Marshal(Request{Method: "TEST", Payload: strings.Repeat("a", messageSize)})
		*requestSize = len(request)

		for i := 0; i < requestsPerClient; i++ {
			timer.Time(func() {
				if err = socket.Send(request); err != nil {
					log.Fatal(err)
				}

				socket.Recv()
			})
		}
	}
}
開發者ID:sguzwf,項目名稱:internal-api-benchmark,代碼行數:32,代碼來源:nanomsg-json.go

示例6: client

func client(url string, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = respondent.NewSocket(); err != nil {
		die("can't get new respondent socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on respondent socket: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("CLIENT(%s): RECEIVED \"%s\" SURVEY REQUEST\n",
			name, string(msg))

		d := date()
		fmt.Printf("CLIENT(%s): SENDING DATE SURVEY RESPONSE\n", name)
		if err = sock.Send([]byte(d)); err != nil {
			die("Cannot send: %s", err.Error())
		}
	}
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:27,代碼來源:survey.go

示例7: server

func server(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = surveyor.NewSocket(); err != nil {
		die("can't get new surveyor socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on surveyor socket: %s", err.Error())
	}
	err = sock.SetOption(mangos.OptionSurveyTime, time.Second*2)
	if err != nil {
		die("SetOption(): %s", err.Error())
	}
	for {
		fmt.Println("SERVER: SENDING DATE SURVEY REQUEST")
		if err = sock.Send([]byte("DATE")); err != nil {
			die("Failed sending survey: %s", err.Error())
		}
		for {
			if msg, err = sock.Recv(); err != nil {
				break
			}
			fmt.Printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n",
				string(msg))
		}
	}
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:30,代碼來源:survey.go

示例8: recv

func recv(s mangos.Socket, obj interface{}) error {
	data, err := s.Recv()
	if err != nil {
		return stackerr.Wrap(err)
	}
	return stackerr.Wrap(json.Unmarshal(data, obj))
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:7,代碼來源:mango_common.go

示例9: StartNanomsgProtoServer

func StartNanomsgProtoServer(port string) {
	var server mangos.Socket
	var err error

	if server, err = rep.NewSocket(); err != nil {
		log.Fatal(err)
	}

	server.AddTransport(tcp.NewTransport())
	server.Listen("tcp://0.0.0.0:" + port)

	for {
		msg, _ := server.Recv()

		var body pb.Request

		proto.Unmarshal(msg, &body)

		data, _ := proto.Marshal(&pb.Response{
			Method:        body.Method,
			PayloadLength: int64(len(body.Payload)),
		})

		server.Send(data)
	}
}
開發者ID:sguzwf,項目名稱:internal-api-benchmark,代碼行數:26,代碼來源:nanomsg-proto.go

示例10: node0

func node0(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = rep.NewSocket(); err != nil {
		die("can't get new rep socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on rep socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		msg, err = sock.Recv()
		if string(msg) == "DATE" { // no need to terminate
			fmt.Println("NODE0: RECEIVED DATE REQUEST")
			d := date()
			fmt.Printf("NODE0: SENDING DATE %s\n", d)
			err = sock.Send([]byte(d))
			if err != nil {
				die("can't send reply: %s", err.Error())
			}
		}
	}
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:26,代碼來源:reqrep.go

示例11: main

func main() {
	flag.Parse()

	var sock mangos.Socket
	var err error
	if sock, err = pub.NewSocket(); err != nil {
		die("can't get new pub socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen("tcp://127.0.0.1:55555"); err != nil {
		die("can't listen on pub socket: %s", err.Error())
	}

	//sub
	var subscriber mangos.Socket

	if subscriber, err = sub.NewSocket(); err != nil {
		die("can't get new sub socket: %s", err.Error())
	}
	subscriber.AddTransport(ipc.NewTransport())
	subscriber.AddTransport(tcp.NewTransport())
	if err = subscriber.Dial("tcp://127.0.0.1:55555"); err != nil {
		die("can't dial on sub socket: %s", err.Error())
	}
	// Empty byte array effectively subscribes to everything
	err = subscriber.SetOption(mangos.OptionSubscribe, []byte(""))

	start := time.Now()

	//var msg []byte
	for i := 1; i <= *numbPtr; i++ {
		//conn.Publish("test", []byte(randSeq(320)))
		sock.Send([]byte(randSeq(320)))

		if _, err = subscriber.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}

		//log.Println(string(msg))
		/*
			runtime.Gosched()
			atomic.AddUint64(&ops, 1)
			if ops == uint64(*numbPtr) {
				elapsed := time.Since(start)
				log.Printf("Time took %s", elapsed)
			}
		*/
	}

	elapsed := time.Since(start)
	log.Printf("Time took %s", elapsed)

	defer sock.Close()

	fmt.Scanln()
}
開發者ID:itmarketplace,項目名稱:goapps,代碼行數:57,代碼來源:pubsub.go

示例12: getEvt

func getEvt(channel mangos.Socket) (string, error) {
	log.Println("getEvt()")
	evt, err := channel.Recv()
	if err != nil {
		return "", err
	}

	return string(evt), nil
}
開發者ID:ordinerf,項目名稱:docker-slim,代碼行數:9,代碼來源:main.go

示例13: startNode

func startNode(masterHost, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	masterUrl := url.URL{Scheme: "tcp", Host: masterHost}
	ip := getPrivateIP()

	// Try to get new "respondent" socket
	if sock, err = respondent.NewSocket(); err != nil {
		utils.Die("Can't get new respondent socket: %s", err.Error())
	}
	defer sock.Close()

	sock.AddTransport(tcp.NewTransport())

	// Connect to master
	if err = sock.Dial(masterUrl.String()); err != nil {
		utils.Die("Can't dial on respondent socket: %s", err.Error())
	}

	// Wait for a survey request and send responses
	for {
		if msg, err = sock.Recv(); err != nil {
			utils.Die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("Client(%s): Received \"%s\" survey request\n", name, string(msg))

		var loadAvg *load.LoadAvgStat
		if loadAvg, err = load.LoadAvg(); err != nil {
			utils.Die("Cannot get load average: %s", err.Error())
		}

		var cpuInfo []cpu.CPUInfoStat
		if cpuInfo, err = cpu.CPUInfo(); err != nil {
			utils.Die("Cannot get CPU info: %s", err.Error())
		}
		fmt.Printf("CPU INFO len: %d\n", len(cpuInfo))

		// Get the normalized CPU load
		avg := loadAvg.Load1
		cores := int32(0)
		for _, info := range cpuInfo {
			fmt.Printf("Inner Cores: %d\n", info.Cores)
			cores += info.Cores
		}
		fmt.Printf("Load avg: %f\n", avg)
		fmt.Printf("Cores: %d\n", cores)
		avg = avg / float64(cores)

		fmt.Printf("Client(%s): Sending survey response\n", name)
		if err = sock.Send([]byte(fmt.Sprintf("%s,%f", ip, avg))); err != nil {
			utils.Die("Cannot send: %s", err.Error())
		}
	}
}
開發者ID:jstol,項目名稱:digital-ocean-autoscaler,代碼行數:55,代碼來源:client.go

示例14: node0

func node0(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = pull.NewSocket(); err != nil {
		die("can't get new pull socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on pull socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		msg, err = sock.Recv()
		fmt.Printf("NODE0: RECEIVED \"%s\"\n", msg)
	}
}
開發者ID:iwarsong,項目名稱:bearded,代碼行數:18,代碼來源:pipeline.go

示例15: Receive

func (receiver *SPReceiver) Receive(address string) error {
	var sock mangos.Socket
	var err error

	if sock, err = rep.NewSocket(); err != nil {
		return err
	}

	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err := sock.Listen(address); err != nil {
		return err
	}

	for {
		if _, err := sock.Recv(); err != nil {
			return err
		}
	}

	return nil
}
開發者ID:pires,項目名稱:go-dojo-scalability-protocols,代碼行數:22,代碼來源:sp_receiver.go


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