Skip to content

architecture and Customization

TsingsonQin edited this page Apr 20, 2019 · 3 revisions

goim architecture

1. goim original architecture

original picture here original architecture

i redraw here for register/discovery detail

original architecture

2. more detail ( logic unit and interface )

architecture degail ( original )

3. goim Customization for your need

architecture degail ( original )

1. instance message storage hooks

code in /internal/logic/dao/kafka.go

// PushMsg push a message to databus.
func (d *Dao) PushMsg(c context.Context, op int32, server string, keys []string, msg []byte) (err error) {
	pushMsg := &pb.PushMsg{
		Type:      pb.PushMsg_PUSH,
		Operation: op,
		Server:    server,
		Keys:      keys,
		Msg:       msg,
	}

        //
        // instance message storage HOOKS:
        // add your message storage logic here
        // if you want to save offline message only, check current user online or not first 
        //

	b, err := proto.Marshal(pushMsg)
	if err != nil {
		return
	}
	m := &sarama.ProducerMessage{
		Key:   sarama.StringEncoder(keys[0]),
		Topic: d.c.Kafka.Topic,
		Value: sarama.ByteEncoder(b),
	}
	if _, _, err = d.kafkaPub.SendMessage(m); err != nil {
		log.Errorf("PushMsg.send(push pushMsg:%v) error(%v)", pushMsg, err)
	}
	return
}

2. user manage hooks

code in /internal/logic/conn.go


// Connect connected a conn.
func (l *Logic) Connect(c context.Context, server, cookie string, token []byte) (mid int64, key, roomID string, accepts []int32, hb int64, err error) {
	var params struct {
		Mid      int64   `json:"mid"`
		Key      string  `json:"key"`
		RoomID   string  `json:"room_id"`
		Platform string  `json:"platform"`
		Accepts  []int32 `json:"accepts"`
	}
	if err = json.Unmarshal(token, &params); err != nil {
		log.Errorf("json.Unmarshal(%s) error(%v)", token, err)
		return
	}
	mid = params.Mid
	roomID = params.RoomID
	accepts = params.Accepts
	hb = int64(l.c.Node.Heartbeat) * int64(l.c.Node.HeartbeatMax)
	// 
        //  User Management HOOKS
        //  add your user management logic here , somethings like:
        //  1. access user management unit to verify mid ( member / user ID ) 
        //  2. check permission to access room or not
        //


        // Session Management HOOKS
        // key just like the session ID, add your session management logic here, like:
        // 1. check session ID is valid or not
        // 2. if not, generate new session ID
        //  example like below: 
	if key = params.Key; key == "" {

		keyUuid, _ := uuid.NewV4()
		key = keyUuid.String()
	}

	//
	if err = l.dao.AddMapping(c, mid, key, server); err != nil {
		log.Errorf("l.dao.AddMapping(%d,%s,%s) error(%v)", mid, key, server, err)
		return
	}
	//
	log.Infof("conn connected key:%s server:%s mid:%d token:%s", key, server, mid, token)
	return
}

this wiki page create by tsingson at china shenzhen, 2019/04/21