| 88 | } |
| 89 | |
| 90 | func OnSubStartHandler(w http.ResponseWriter, r *http.Request) { |
| 91 | id := unique.GenUniqueKey("ReqID") |
| 92 | |
| 93 | var info base.SubStartInfo |
| 94 | if err := nazahttp.UnmarshalRequestJsonBody(r, &info); err != nil { |
| 95 | nazalog.Error(err) |
| 96 | return |
| 97 | } |
| 98 | nazalog.Infof("[%s] on_sub_start. info=%+v", id, info) |
| 99 | |
| 100 | // 演示通过流名称踢掉session,服务于鉴权等场景 |
| 101 | // 业务方真正使用时,可以通过流名称、用户IP、URL参数等信息,来判断是否需要踢掉session |
| 102 | if info.StreamName == "cheftestkick" { |
| 103 | reqServer, exist := config.ServerId2Server[info.ServerId] |
| 104 | if !exist { |
| 105 | nazalog.Errorf("[%s] req server id invalid.", id) |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | url := fmt.Sprintf("http://%s/api/ctrl/kick_session", reqServer.ApiAddr) |
| 110 | var b base.ApiCtrlKickSessionReq |
| 111 | b.StreamName = info.StreamName |
| 112 | b.SessionId = info.SessionId |
| 113 | |
| 114 | nazalog.Infof("[%s] ctrl kick out session. send to %s with %+v", id, reqServer.ApiAddr, b) |
| 115 | if _, err := nazahttp.PostJson(url, b, nil); err != nil { |
| 116 | nazalog.Errorf("[%s] post json error. err=%+v", id, err) |
| 117 | } |
| 118 | return |
| 119 | } |
| 120 | |
| 121 | // sub拉流时,判断是否需要触发pull级联拉流 |
| 122 | // 1. 是内部级联拉流,不需要触发 |
| 123 | if strings.Contains(info.UrlParam, config.PullSecretParam) { |
| 124 | nazalog.Infof("[%s] sub is pull by other node, ignore.", id) |
| 125 | return |
| 126 | } |
| 127 | // 2. 汇报的节点已经存在输入流,不需要触发 |
| 128 | if info.HasInSession { |
| 129 | nazalog.Infof("[%s] in not empty, ignore.", id) |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | // 3. 非法节点,本服务没有配置汇报的节点 |
| 134 | reqServer, exist := config.ServerId2Server[info.ServerId] |
| 135 | if !exist { |
| 136 | nazalog.Errorf("[%s] req server id invalid.", id) |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | pubServerId, exist := dataManager.QueryPub(info.StreamName) |
| 141 | // 4. 没有查到流所在节点,不需要触发 |
| 142 | if !exist { |
| 143 | nazalog.Infof("[%s] pub not exist, ignore.", id) |
| 144 | return |
| 145 | } |
| 146 | |
| 147 | pubServer, exist := config.ServerId2Server[pubServerId] |