@@ -66,68 +66,53 @@ The sample below demonstrates how to listen to media stream using your websocket
66
66
HttpListener httpListener = new HttpListener ();
67
67
httpListener .Prefixes .Add (" http://localhost:80/" );
68
68
httpListener .Start ();
69
- while (true ) {
70
- HttpListenerContext httpListenerContext = await httpListener .GetContextAsync ();
71
- if (httpListenerContext .Request .IsWebSocketRequest ) {
72
- WebSocketContext websocketContext ;
73
- try {
69
+ while (true )
70
+ {
71
+ HttpListenerContext httpListenerContext = await httpListener .GetContextAsync ();
72
+ if (httpListenerContext .Request .IsWebSocketRequest )
73
+ {
74
+ WebSocketContext websocketContext ;
75
+ try
76
+ {
74
77
websocketContext = await httpListenerContext .AcceptWebSocketAsync (subProtocol : null );
75
- string ipAddress = httpListenerContext .Request .RemoteEndPoint .Address .ToString ();
76
- } catch (Exception ex ) {
77
- httpListenerContext .Response .StatusCode = 500 ;
78
- httpListenerContext .Response .Close ();
79
- return ;
80
- }
78
+ }
79
+ catch (Exception ex )
80
+ {
81
+ return ;
82
+ }
81
83
WebSocket webSocket = websocketContext .WebSocket ;
82
- try {
83
- while (webSocket .State == WebSocketState .Open || webSocket .State == WebSocketState .CloseSent ) {
84
+ try
85
+ {
86
+ while (webSocket .State == WebSocketState .Open || webSocket .State == WebSocketState .CloseSent )
87
+ {
84
88
byte [] receiveBuffer = new byte [2048 ];
85
89
var cancellationToken = new CancellationTokenSource (TimeSpan .FromSeconds (60 )).Token ;
86
- WebSocketReceiveResult receiveResult = await webSocket .ReceiveAsync (new ArraySegment < byte > . (receiveBuffer ), cancellationToken );
87
- if (receiveResult .MessageType != WebSocketMessageType .Close ) {
88
- var data = Encoding .UTF8 .GetString (receiveBuffer ).TrimEnd ('\0 ' );
89
- try {
90
- var json = JsonConvert .DeserializeObject < Audio > (data );
91
- if (json != null ) {
92
- var byteArray = json .AudioData ;
93
- // Processing mixed audio data
94
- if (string .IsNullOrEmpty (json ? .ParticipantId )) {
95
- if (string .IsNullOrEmpty (WebSocketData .FirstReceivedMixedAudioBufferTimeStamp )) {
96
- WebSocketData .FirstReceivedMixedAudioBufferTimeStamp = json .Timestamp ;
97
- }
98
- // Process byteArray ( audioData ) however you want
99
- }
100
- }
101
-
102
- // Processing unmixed audio data
103
- else if (! string .IsNullOrEmpty (json ? .ParticipantId ) && ! json .IsSilence ) {
104
- if (json .ParticipantId != null ) {
105
- switch (json .ParticipantId ) {
106
- case {
107
- participantRawId1
108
- }:
109
- // Process audio data
110
- break ;
111
- case {
112
- participantRawId2
113
- }::
114
- // Process audio data
115
- break ;
116
- default :
117
- break ;
118
- }
119
- }
120
- if (string .IsNullOrEmpty (WebSocketData .FirstReceivedUnmixedAudioBufferTimeStamp )) {
121
- WebSocketData .FirstReceivedUnmixedAudioBufferTimeStamp = json .Timestamp ;
90
+ WebSocketReceiveResult receiveResult = await webSocket .ReceiveAsync (new ArraySegment <byte >(receiveBuffer ), cancellationToken );
91
+ if (receiveResult .MessageType != WebSocketMessageType .Close )
92
+ {
93
+ var data = Encoding .UTF8 .GetString (receiveBuffer ).TrimEnd ('\0 ' );
94
+ try
95
+ {
96
+ var eventData = JsonConvert .DeserializeObject <AudioBaseClass >(data );
97
+ if (eventData != null )
98
+ {
99
+ if (eventData .kind == " AudioMetadata" )
100
+ {
101
+ // Process audio metadata
102
+ }
103
+ else if (eventData .kind == " AudioData" )
104
+ {
105
+ // Process audio data
106
+ var byteArray = eventData .audioData .data ;
107
+ // use audio byteArray as you want
108
+ }
122
109
}
123
110
}
124
- } catch {}
125
- }
126
- }
127
- } catch (Exception ex ) {}
128
- } else {
129
- httpListenerContext .Response .StatusCode = 400 ;
130
- httpListenerContext .Response .Close ();
131
- }
111
+ catch { }
112
+ }
113
+ }
114
+ }
115
+ catch (Exception ex ) { }
116
+ }
132
117
}
133
118
```
0 commit comments