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


Golang Context.NewSocket方法代码示例

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


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

示例1: monitor

func (server *Server) monitor(ctx *zmq.Context, addr string) {
	sock, err := ctx.NewSocket(zmq.PAIR)
	if err != nil {
		glog.Fatal(err)
	}

	if err := sock.Connect(addr); err != nil {
		glog.Fatal(err)
	}
	defer sock.Close()

	var lock sync.Mutex
	var numClients int

	for {
		evtType, _, _, err := sock.RecvEvent(0)
		if err != nil {
			glog.Error(err)
			break
		}

		switch zmq.Event(evtType) {
		case zmq.EVENT_ACCEPTED:
			lock.Lock()
			numClients++
			glog.Infof("Client #%d connected", numClients)
			lock.Unlock()
		case zmq.EVENT_DISCONNECTED:
			lock.Lock()
			glog.Infof("Client #%d disconnected", numClients)
			numClients--
			lock.Unlock()
		}
	}
}
开发者ID:gust1n,项目名称:zrpc,代码行数:35,代码来源:server.go

示例2: InitSocket

func (this *Spout) InitSocket(pull, push string) (err error) {
	var reader, writer *zmq.Context
	reader, err = zmq.NewContext()
	if err != nil {
		return
	}

	this.Component.Reader, err = reader.NewSocket(zmq.PULL)
	if err != nil {
		return
	}

	err = this.Component.Reader.Connect("tcp://127.0.0.1:" + pull)
	if err != nil {
		return
	}

	writer, err = zmq.NewContext()
	if err != nil {
		return
	}

	this.Component.Writer, err = writer.NewSocket(zmq.PUSH)
	if err != nil {
		return
	}

	err = this.Component.Writer.Bind("tcp://127.0.0.1:" + push)
	return
}
开发者ID:HearingFish,项目名称:gostorm,代码行数:30,代码来源:spout.go

示例3: NewSocketAndAddress

func NewSocketAndAddress(c *zmq.Context, bind bool, t zmq.Type, name string) (a *zmq.Socket, addr string, err error) {
	addr = fmt.Sprintf("inproc://_routerelement_internal-%d", getUniqueId())
	if a, err = c.NewSocket(t); err != nil {
		goto Error
	}
	if name != "" {
		a.SetIdentity(name)
	}
	if bind {
		if err = a.Bind(addr); err != nil {
			goto Error
		}
	} else {
		if err = a.Connect(addr); err != nil {
			goto Error
		}
	}
	return

Error:
	if a != nil {
		a.Close()
		a = nil
	}
	addr = ""
	return
}
开发者ID:abligh,项目名称:ghostfish,代码行数:27,代码来源:routerelement.go

示例4: NewPair

func NewPair(c *zmq.Context) (a *zmq.Socket, b *zmq.Socket, err error) {
	addr := fmt.Sprintf("inproc://_routerelement_internal-%d", getUniqueId())
	if a, err = c.NewSocket(zmq.PAIR); err != nil {
		goto Error
	}
	if err = a.Bind(addr); err != nil {
		goto Error
	}
	if b, err = c.NewSocket(zmq.PAIR); err != nil {
		goto Error
	}
	if err = b.Connect(addr); err != nil {
		goto Error
	}
	return

Error:
	if a != nil {
		a.Close()
		a = nil
	}
	if b != nil {
		b.Close()
		b = nil
	}
	return
}
开发者ID:abligh,项目名称:ghostfish,代码行数:27,代码来源:routerelement.go

示例5: NewClientWithConnection

// NewClientWithConnection returns a new Client to handle requests to the
// set of services at the other end of the connection.
// An existing connection in the form of a zmq socket together with the zmq
// context they were created with is used
func NewClientWithConnection(ctx *zmq.Context, conn *zmq.Socket) *Client {
	// A router socket is the middle-man between client requests and actually sending/receiving on the wire
	router, err := ctx.NewSocket(zmq.ROUTER)
	if err != nil {
		glog.Fatal(err)
	}
	if err := router.Bind(RouterURL); err != nil {
		glog.Fatal(err)
	}

	client := &Client{
		conn: conn,
		endpoints: endpoints{
			socket: conn,
		},
		router: router,
		ctx:    ctx,
	}

	// Start the proxy in an own routine since it is blocking
	go func() {
		if err := zmq.Proxy(conn, router, nil); err != nil {
			switch zmq.AsErrno(err) {
			case zmq.Errno(zmq.ETERM):
				glog.Info(err)
			case zmq.Errno(syscall.EINTR):
				glog.Info(err)
			default:
				glog.Info(zmq.AsErrno(err))
				glog.Info(err)
			}
			client.Close()
		}
	}()

	// Socket monitor for connect event
	monitorURL := "inproc://monitor"
	if err := conn.Monitor(monitorURL, zmq.EVENT_CONNECTED|zmq.EVENT_DISCONNECTED); err != nil {
		client.Close()
		glog.Fatal(err)
	}
	go client.monitor(monitorURL)

	return client
}
开发者ID:gust1n,项目名称:zrpc,代码行数:49,代码来源:client.go

示例6: TestMultipleContexts

func TestMultipleContexts(t *testing.T) {

	chQuit := make(chan interface{})
	chErr := make(chan error, 2)
	needQuit := false
	var sock1, sock2, serv1, serv2 *zmq.Socket
	var serv_ctx1, serv_ctx2, ctx1, ctx2 *zmq.Context
	var err error

	defer func() {
		if needQuit {
			chQuit <- true
			chQuit <- true
			<-chErr
			<-chErr
		}
		for _, s := range []*zmq.Socket{sock1, sock2, serv1, serv2} {
			if s != nil {
				s.SetLinger(0)
				s.Close()
			}
		}
		for _, c := range []*zmq.Context{serv_ctx1, serv_ctx2, ctx1, ctx2} {
			if c != nil {
				c.Term()
			}
		}
	}()

	addr1 := "tcp://127.0.0.1:9997"
	addr2 := "tcp://127.0.0.1:9998"

	serv_ctx1, err = zmq.NewContext()
	if err != nil {
		t.Fatal("NewContext:", err)
	}
	serv1, err = serv_ctx1.NewSocket(zmq.REP)
	if err != nil {
		t.Fatal("NewSocket:", err)
	}
	err = serv1.Bind(addr1)
	if err != nil {
		t.Fatal("Bind:", err)
	}

	serv_ctx2, err = zmq.NewContext()
	if err != nil {
		t.Fatal("NewContext:", err)
	}
	serv2, err = serv_ctx2.NewSocket(zmq.REP)
	if err != nil {
		t.Fatal("NewSocket:", err)
	}
	err = serv2.Bind(addr2)
	if err != nil {
		t.Fatal("Bind:", err)
	}

	new_service := func(sock *zmq.Socket, addr string) {
		socket_handler := func(state zmq.State) error {
			msg, err := sock.RecvMessage(0)
			if err != nil {
				return err
			}
			_, err = sock.SendMessage(addr, msg)
			return err
		}
		quit_handler := func(interface{}) error {
			return errors.New("quit")
		}

		reactor := zmq.NewReactor()
		reactor.AddSocket(sock, zmq.POLLIN, socket_handler)
		reactor.AddChannel(chQuit, 1, quit_handler)
		err = reactor.Run(100 * time.Millisecond)
		chErr <- err
	}

	go new_service(serv1, addr1)
	go new_service(serv2, addr2)
	needQuit = true

	time.Sleep(time.Second)

	// default context

	sock1, err = zmq.NewSocket(zmq.REQ)
	if err != nil {
		t.Fatal("NewSocket:", err)
	}
	sock2, err = zmq.NewSocket(zmq.REQ)
	if err != nil {
		t.Fatal("NewSocket:", err)
	}
	err = sock1.Connect(addr1)
	if err != nil {
		t.Fatal("sock1.Connect:", err)
	}
	err = sock2.Connect(addr2)
	if err != nil {
//.........这里部分代码省略.........
开发者ID:pebbe,项目名称:zmq4,代码行数:101,代码来源:zmq4_test.go


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