receive 用于接收发布或者订阅
(streamPath string, specific common.IIO)
| 170 | |
| 171 | // receive 用于接收发布或者订阅 |
| 172 | func (io *IO) receive(streamPath string, specific common.IIO) error { |
| 173 | streamPath = strings.Trim(streamPath, "/") |
| 174 | u, err := url.Parse(streamPath) |
| 175 | if err != nil { |
| 176 | if EngineConfig.LogLang == "zh" { |
| 177 | log.Error("接收流路径(流唯一标识)格式错误,必须形如 live/test ", zap.String("流路径", streamPath), zap.Error(err)) |
| 178 | } else { |
| 179 | log.Error("receive streamPath wrong format", zap.String("streamPath", streamPath), zap.Error(err)) |
| 180 | } |
| 181 | return err |
| 182 | } |
| 183 | io.Args = u.Query() |
| 184 | wt := time.Second * 5 |
| 185 | var iSub ISubscriber |
| 186 | var iPub IPublisher |
| 187 | var isSubscribe bool |
| 188 | if iSub, isSubscribe = specific.(ISubscriber); isSubscribe { |
| 189 | wt = iSub.GetSubscriber().Config.WaitTimeout |
| 190 | } else { |
| 191 | iPub = specific.(IPublisher) |
| 192 | } |
| 193 | s, create := findOrCreateStream(u.Path, wt) |
| 194 | if s == nil { |
| 195 | return ErrBadStreamName |
| 196 | } |
| 197 | if io.Stream == nil { //初次 |
| 198 | if io.Type == "" { |
| 199 | io.Type = reflect.TypeOf(specific).Elem().Name() |
| 200 | } |
| 201 | logFeilds := []zapcore.Field{zap.String("type", io.Type)} |
| 202 | if io.ID != "" { |
| 203 | logFeilds = append(logFeilds, zap.String("ID", io.ID)) |
| 204 | } |
| 205 | if io.Logger == nil { |
| 206 | io.Logger = s.With(logFeilds...) |
| 207 | } else { |
| 208 | io.Logger = io.Logger.With(logFeilds...) |
| 209 | } |
| 210 | } |
| 211 | io.Stream = s |
| 212 | io.Spesific = specific |
| 213 | io.StartTime = time.Now() |
| 214 | if io.Context == nil { |
| 215 | io.Debug("create context") |
| 216 | io.SetParentCtx(Engine.Context) |
| 217 | } else if io.IsClosed() { |
| 218 | io.Debug("recreate context") |
| 219 | io.SetParentCtx(Engine.Context) |
| 220 | } else { |
| 221 | io.Debug("warp context") |
| 222 | io.SetParentCtx(io.Context) |
| 223 | } |
| 224 | defer func() { |
| 225 | if err == nil { |
| 226 | specific.OnEvent(specific) |
| 227 | } |
| 228 | }() |
| 229 | if !isSubscribe { |
no test coverage detected