public async Task StartReceive() { quitFlag = false; while (!quitFlag) { List <byte[]> datas; bool succeed; if (_dataChannel) { succeed = _rtsp.TryDequeData(out datas, 10); } else { succeed = _rtsp.TryDequeControl(out datas, 1); } if (succeed) { foreach (byte[] data in datas) { if (!_dataChannel) { WSRtspResponse response = new WSRtspResponse(); response.Seq = Seq; response.RtspBuffer = data; await _ws.SendAsync(response.ToArray(), _dataChannel?WebSocketMessageType.Binary : WebSocketMessageType.Text, true, CancellationToken.None); } else { await _ws.SendAsync(data, _dataChannel?WebSocketMessageType.Binary : WebSocketMessageType.Text, true, CancellationToken.None); } } } else { if (_dataChannel) { await Task.Delay(1); } else { await Task.Delay(10); } } } }
public override async Task ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, byte[] buffer) { if (buffer == null || buffer.Length == 0) { return; } var socketId = WebSocketConnectionManager.GetId(socket); WSRtspContext wsrtsp = null; if (ws_rtsps.ContainsKey(socketId)) { wsrtsp = ws_rtsps[socketId]; } if (result.MessageType == WebSocketMessageType.Text) { string package = Encoding.UTF8.GetString(buffer); string command = getWSPCommand(package); string seq = getByKey(package, "seq"); if (command == "INIT")//建立新链接. { string host = getByKey(package, "host"); string port = getByKey(package, "port"); if (port == null) { port = "554"; } if (host != null) { try { RtspProxy rtsp = new RtspProxy(); bool connected = rtsp.Connect(host, Convert.ToInt32(port)); if (connected) { rtsp.Start(); wsrtsp = new WSRtspContext(socket, rtsp, false); wsrtsp.ControlWebSocketId = socketId; ws_rtsps.TryAdd(socketId, wsrtsp); wsrtsp.Seq = seq; //返回握手. WSRtspResponse response = new WSRtspResponse(); response.Seq = seq; response.Shakehand = true; response.Channel = socketId; await socket.SendAsync(response.ToArray(), WebSocketMessageType.Text, true, CancellationToken.None); //启动接受rtsp控制报文,发送给ws. await wsrtsp.StartReceive(); } else { await removeSocket(wsrtsp); return; } } catch (Exception ex) { //Logger.Error(String.Format("connect to rtsp {0} Error:{1}", package, ex.Message)); return; } } } else if (command == "JOIN")//建立数据通道. { string channel = getByKey(package, "channel"); if (channel != null && ws_rtsps.ContainsKey(channel)) { WSRtspContext controlwsrtsp = ws_rtsps[channel]; controlwsrtsp.DataWebSocketId = socketId; WSRtspContext datawsrtsp = new WSRtspContext(socket, controlwsrtsp.Rtsp, true); datawsrtsp.ControlWebSocketId = controlwsrtsp.ControlWebSocketId; datawsrtsp.DataWebSocketId = socketId; ws_rtsps.TryAdd(socketId, datawsrtsp); //返回握手. WSRtspResponse response = new WSRtspResponse(); response.Seq = seq; await socket.SendAsync(response.ToArray(), WebSocketMessageType.Text, true, CancellationToken.None); //启动接受rtsp数据报文,发送给ws. await datawsrtsp.StartReceive(); } return; }//WSP/1.1 JOIN channel: 127.0.0.1 - 2 18467 seq: 3 else { wsrtsp.Seq = seq; try { await wsrtsp.Send(getRtspBuffer(package)); } catch { await removeSocket(wsrtsp); } } } else if (wsrtsp != null) { try { await wsrtsp.Send(buffer); } catch { await removeSocket(wsrtsp); } }//WebSocketMessageType.Data }