Exemple #1
0
func StartGateConnector(pipe cellnet.EventPipe, addressList []string) {

	gateConnArray = make([]cellnet.Peer, len(addressList))

	for index, addr := range addressList {

		conn := socket.NewConnector(pipe).Start(addr)
		gateConnArray[index] = conn

		gateIndex := new(int)
		*gateIndex = index

		// 广播
		socket.RegisterSessionMessage(conn, coredef.UpstreamACK{}, func(content interface{}, ses cellnet.Session) {
			msg := content.(*coredef.UpstreamACK)

			// 生成派发的消息

			// TODO 用PostData防止多重嵌套?
			// 调用已注册的回调
			conn.CallData(&relayEvent{
				SessionEvent: socket.NewSessionEvent(msg.GetMsgID(), ses, msg.Data),
				ClientID:     msg.GetClientID(),
			})

		})

	}

}
Exemple #2
0
// 后台服务器到router的连接
func StartBackendConnector(pipe cellnet.EventPipe, addressList []string, peerName string, svcName string) {

	routerConnArray = make([]cellnet.Peer, len(addressList))

	if len(addressList) == 0 {
		log.Warnf("empty router address list")
		return
	}

	for index, addr := range addressList {

		peer := socket.NewConnector(pipe)
		peer.SetName(peerName)

		peer.(cellnet.Connector).SetAutoReconnectSec(defaultReconnectSec)

		peer.Start(addr)

		routerConnArray[index] = peer

		// 连上网关时, 发送自己的服务器名字进行注册
		socket.RegisterSessionMessage(peer, "coredef.SessionConnected", func(content interface{}, ses cellnet.Session) {

			ses.Send(&coredef.RegisterRouterBackendACK{
				Name: svcName,
			})

		})

		// 广播
		socket.RegisterSessionMessage(peer, "coredef.UpstreamACK", func(content interface{}, ses cellnet.Session) {
			msg := content.(*coredef.UpstreamACK)

			// 生成派发的消息

			// TODO 用PostData防止多重嵌套?
			// 调用已注册的回调
			peer.CallData(&relayEvent{
				SessionEvent: socket.NewSessionEvent(msg.MsgID, ses, msg.Data),
				ClientID:     msg.ClientID,
			})

		})

	}

}