Ejemplo n.º 1
0
//create connection pool and tests
func main() {

	cpool1 := SpawnConnectionPool(6060)
	cpool2 := SpawnConnectionPool(6061)

	//connect to peer
	con, err := cpool1.Connect("127.0.0.1:6061")
	_ = con

	if err != nil {
		log.Panic(err)
	}

	//new dispatch manager
	dm1 := gnet.NewDispatcherManager()
	cpool1.Config.MessageCallback = dm1.OnMessage //set message handler
	//new dispatcher for handling messages on channel 3
	d1 := dm1.NewDispatcher(cpool1, 3, nil) //dispatcher 1
	d1.RegisterMessages(messageMap)

	dm2 := gnet.NewDispatcherManager()
	cpool2.Config.MessageCallback = dm2.OnMessage
	d2 := dm2.NewDispatcher(cpool2, 3, nil) //dispatcher 2
	d2.RegisterMessages(messageMap)

	//create a message to send
	tm := TestMessage{Text: []byte("Message test")}
	d1.SendMessage(con, 3, &tm)

	time.Sleep(time.Second * 10)
}
Ejemplo n.º 2
0
func (self *PoolOwner) Init(pCMan *MinimalConnectionManager, listen_port uint16, num_id int, nickname string) {

	config := gnet.NewConfig()
	config.Port = uint16(listen_port)

	cp := gnet.NewConnectionPool(config)

	dm := gnet.NewDispatcherManager()

	self.isConnSolicited = make(map[string]bool)

	cp.Config.MessageCallback = dm.OnMessage
	cp.Config.ConnectCallback = self.OnConnect
	cp.Config.DisconnectCallback = self.OnDisconnect

	d := dm.NewDispatcher(cp, common_channel, self)
	d.RegisterMessages(messageMap)

	self.pCMan = pCMan
	self.pDispatcherManager = dm
	self.pDispatcher = d
	self.key_map = make(map[string]byte)
	self.pConnectionPool = cp

	self.debug_num_id = num_id
	self.debug_nickname = nickname
}