func (p *Peer) Join(msg *proto.FromClientJoinMsg) (interface{}, error) {
	log.Infof("peer join: uid=%s, msg=%v", p.uid, msg)

	p.sid = msg.SID
	p.info = msg.Info

	// validate
	if p.sid == "" {
		return nil, errors.New("room not found")
	}

	// join room
	p.s.addPeer(p.sid, p)
	log.Errorf("[%s] join to room %s", p.uid, p.sid)

	// get sfu node
	sfu, err := p.sfu()
	if err != nil {
		p.s.delPeer(p.sid, p.uid)
		log.Errorf("[%s] sfu not found: %v", p.uid, err)
		return nil, errors.New("sfu not found")
	}

	// join to sfu
	resp, err := p.s.nrpc.Request(sfu, proto.ToSfuJoinMsg{
		RPC:   p.s.nid,
		MID:   p.mid,
		UID:   p.uid,
		SID:   p.sid,
		Offer: msg.Offer,
	})
	if err != nil {
		p.s.delPeer(p.sid, p.uid)
		log.Errorf("[%s] join to %s error: %v", p.uid, sfu, err)
		return nil, errors.New("join to sfu error")
	}
	fromSfuJoinMsg := resp.(*proto.FromSfuJoinMsg)

	// join to islb
	resp, err = p.s.nrpc.Request(p.s.islb, proto.ToIslbPeerJoinMsg{
		UID: p.uid, SID: p.sid, MID: p.mid, Info: p.info,
	})
	if err != nil {
		if _, err := p.s.nrpc.Request(sfu, proto.ToSfuLeaveMsg{
			MID: p.mid,
		}); err != nil {
			log.Errorf("[%s] leave %s error: %v", p.uid, sfu, err.Error())
		}
		p.s.delPeer(p.sid, p.uid)
		log.Errorf("[%s] join to %s error: %v", p.uid, p.s.islb, err)
		return nil, errors.New("join to sfu error")
	}

	// send peer-list to clients
	fromIslbPeerJoinMsg := resp.(*proto.FromIslbPeerJoinMsg)
	go func(peerlist proto.ToClientPeersMsg) {
		time.Sleep(100 * time.Millisecond)
		if err := p.send(&peerlist); err != nil {
			log.Errorf("[%s] send peer-list to clients error: %v", p.uid, err)
		}
	}(proto.ToClientPeersMsg{
		Peers:   fromIslbPeerJoinMsg.Peers,
		Streams: fromIslbPeerJoinMsg.Streams,
	})

	return fromSfuJoinMsg.Answer, nil
}
// Join client Join the room
Join client Join the room
// Join client Join the room
func (p *Peer) Join(msg *proto.FromClientJoinMsg) (interface{}, error) {
	log.Infof("peer join: uid=%s, msg=%v", p.uid, msg)
	p.sid = msg.SID
	p.info = msg.Info
	if p.sid == "" {
		return nil, errors.New("room not found")
	}
	p.s.addPeer(p.sid, p)
	log.Errorf("[%s] join to room %s", p.uid, p.sid)
	sfu, err := p.sfu()
	if err != nil {
		p.s.delPeer(p.sid, p.uid)
		log.Errorf("[%s] sfu not found: %v", p.uid, err)
		return nil, errors.New("sfu not found")
	}
	resp, err := p.s.nrpc.Request(sfu, proto.ToSfuJoinMsg{
		RPC:   p.s.nid,
		MID:   p.mid,
		UID:   p.uid,
		SID:   p.sid,
		Offer: msg.Offer,
	})
	if err != nil {
		p.s.delPeer(p.sid, p.uid)
		log.Errorf("[%s] join to %s error: %v", p.uid, sfu, err)
		return nil, errors.New("join to sfu error")
	}
	fromSfuJoinMsg := resp.(*proto.FromSfuJoinMsg)
	resp, err = p.s.nrpc.Request(p.s.islb, proto.ToIslbPeerJoinMsg{
		UID: p.uid, SID: p.sid, MID: p.mid, Info: p.info,
	})
	if err != nil {
		if _, err := p.s.nrpc.Request(sfu, proto.ToSfuLeaveMsg{
			MID: p.mid,
		}); err != nil {
			log.Errorf("[%s] leave %s error: %v", p.uid, sfu, err.Error())
		}
		p.s.delPeer(p.sid, p.uid)
		log.Errorf("[%s] join to %s error: %v", p.uid, p.s.islb, err)
		return nil, errors.New("join to sfu error")
	}
	fromIslbPeerJoinMsg := resp.(*proto.FromIslbPeerJoinMsg)
	go func(peerlist proto.ToClientPeersMsg) {
		time.Sleep(100 * time.Millisecond)
		if err := p.send(&peerlist); err != nil {
			log.Errorf("[%s] send peer-list to clients error: %v", p.uid, err)
		}
	}(proto.ToClientPeersMsg{
		Peers:   fromIslbPeerJoinMsg.Peers,
		Streams: fromIslbPeerJoinMsg.Streams,
	})
	return fromSfuJoinMsg.Answer, nil
}
func ( p * Peer ) Join ( msg * proto . FromClientJoinMsg ) ( interface { } , error ) { log . Infof ( " " , p . uid , msg ) p . sid = msg . SID p . info = msg . Info if p . sid == " " { return nil , errors . New ( " " ) } p . s . addPeer ( p . sid , p ) log . Errorf ( " " , p . uid , p . sid ) sfu , err := p . sfu ( ) if err != nil { p . s . delPeer ( p . sid , p . uid ) log . Errorf ( " " , p . uid , err ) return nil , errors . New ( " " ) } resp , err := p . s . nrpc . Request ( sfu , proto . ToSfuJoinMsg { RPC : p . s . nid , MID : p . mid , UID : p . uid , SID : p . sid , Offer : msg . Offer , } ) if err != nil { p . s . delPeer ( p . sid , p . uid ) log . Errorf ( " " , p . uid , sfu , err ) return nil , errors . New ( " " ) } fromSfuJoinMsg := resp . ( * proto . FromSfuJoinMsg ) resp , err = p . s . nrpc . Request ( p . s . islb , proto . ToIslbPeerJoinMsg { UID : p . uid , SID : p . sid , MID : p . mid , Info : p . info , } ) if err != nil { if _ , err := p . s . nrpc . Request ( sfu , proto . ToSfuLeaveMsg { MID : p . mid , } ) ; err != nil { log . Errorf ( " " , p . uid , sfu , err . Error ( ) ) } p . s . delPeer ( p . sid , p . uid ) log . Errorf ( " " , p . uid , p . s . islb , err ) return nil , errors . New ( " " ) } fromIslbPeerJoinMsg := resp . ( * proto . FromIslbPeerJoinMsg ) go func ( peerlist proto . ToClientPeersMsg ) { time . Sleep ( 100 * time . Millisecond ) if err := p . send ( & peerlist ) ; err != nil { log . Errorf ( " " , p . uid , err ) } } ( proto . ToClientPeersMsg { Peers : fromIslbPeerJoinMsg . Peers , Streams : fromIslbPeerJoinMsg . Streams , } ) return fromSfuJoinMsg . Answer , nil }
Join client Join the room
Join client Join the room
// validate // join room // get sfu node // join to sfu // join to islb // send peer-list to clients