@@ -31,18 +31,18 @@ public AudioStreamer (string url)
3131 configure ( ) ;
3232 }
3333
34- private AudioMessage acceptBinaryMessage ( byte [ ] value )
34+ private AudioMessage acceptBinaryMessage ( byte [ ] data )
3535 {
36- var id = value . SubArray ( 0 , 4 ) . To < uint > ( ByteOrder . Big ) ;
37- var chNum = value . SubArray ( 4 , 1 ) [ 0 ] ;
38- var bufferLength = value . SubArray ( 5 , 4 ) . To < uint > ( ByteOrder . Big ) ;
36+ var id = data . SubArray ( 0 , 4 ) . To < uint > ( ByteOrder . Big ) ;
37+ var chNum = data . SubArray ( 4 , 1 ) [ 0 ] ;
38+ var bufferLength = data . SubArray ( 5 , 4 ) . To < uint > ( ByteOrder . Big ) ;
3939 var bufferArray = new float [ chNum , bufferLength ] ;
4040
4141 var offset = 9 ;
4242 ( ( int ) chNum ) . Times (
4343 i => bufferLength . Times (
4444 j => {
45- bufferArray [ i , j ] = value . SubArray ( offset , 4 ) . To < float > ( ByteOrder . Big ) ;
45+ bufferArray [ i , j ] = data . SubArray ( offset , 4 ) . To < float > ( ByteOrder . Big ) ;
4646 offset += 4 ;
4747 } ) ) ;
4848
@@ -54,37 +54,37 @@ private AudioMessage acceptBinaryMessage (byte [] value)
5454 } ;
5555 }
5656
57- private NotificationMessage acceptTextMessage ( string value )
57+ private NotificationMessage acceptTextMessage ( string data )
5858 {
59- var json = JObject . Parse ( value ) ;
59+ var json = JObject . Parse ( data ) ;
6060 var id = ( uint ) json [ "user_id" ] ;
6161 var name = ( string ) json [ "name" ] ;
6262 var type = ( string ) json [ "type" ] ;
6363
6464 string message ;
65- if ( type == "connection" ) {
65+ if ( type == "message" )
66+ message = String . Format ( "{0}: {1}" , name , ( string ) json [ "message" ] ) ;
67+ else if ( type == "start_music" )
68+ message = String . Format ( "{0}: Started playing music!" , name ) ;
69+ else if ( type == "connection" ) {
6670 var users = ( JArray ) json [ "message" ] ;
67- var msg = new StringBuilder ( "Now keeping connection :" ) ;
71+ var msg = new StringBuilder ( "Now keeping connections :" ) ;
6872 foreach ( JToken user in users )
6973 msg . AppendFormat (
7074 "\n - user_id: {0} name: {1}" , ( uint ) user [ "user_id" ] , ( string ) user [ "name" ] ) ;
7175
7276 message = msg . ToString ( ) ;
7377 }
7478 else if ( type == "connected" ) {
75- _heartbeatTimer . Change ( 30000 , 30000 ) ;
7679 _id = id ;
80+ _heartbeatTimer . Change ( 30000 , 30000 ) ;
7781 message = String . Format ( "user_id: {0} name: {1}" , id , name ) ;
7882 }
79- else if ( type == "message" )
80- message = String . Format ( "{0}: {1}" , name , ( string ) json [ "message" ] ) ;
81- else if ( type == "start_music" )
82- message = String . Format ( "{0}: Started playing music!" , name ) ;
8383 else
8484 message = "Received unknown type message." ;
8585
8686 return new NotificationMessage {
87- Summary = String . Format ( "AudioStreamer Message ({0})" , type ) ,
87+ Summary = String . Format ( "AudioStreamer ({0})" , type ) ,
8888 Body = message ,
8989 Icon = "notification-message-im"
9090 } ;
@@ -120,16 +120,16 @@ private void configure ()
120120 _websocket . OnError += ( sender , e ) =>
121121 _notifier . Notify (
122122 new NotificationMessage {
123- Summary = "AudioStreamer Error " ,
123+ Summary = "AudioStreamer (error) " ,
124124 Body = e . Message ,
125125 Icon = "notification-message-im"
126126 } ) ;
127127
128128 _websocket . OnClose += ( sender , e ) =>
129129 _notifier . Notify (
130130 new NotificationMessage {
131- Summary = String . Format ( "AudioStreamer Disconnect ({0})" , e . Code ) ,
132- Body = e . Reason ,
131+ Summary = "AudioStreamer (disconnect)" ,
132+ Body = String . Format ( "code: {0} reason: {1}" , e . Code , e . Reason ) ,
133133 Icon = "notification-message-im"
134134 } ) ;
135135 }
@@ -169,25 +169,19 @@ private void sendHeartbeat (object state)
169169 _websocket . Send ( createTextMessage ( "heartbeat" , String . Empty ) ) ;
170170 }
171171
172- public void Connect ( )
172+ public void Connect ( string username )
173173 {
174- do {
175- Console . Write ( "Input your name> " ) ;
176- _name = Console . ReadLine ( ) ;
177- }
178- while ( _name . Length == 0 ) ;
179-
174+ _name = username ;
180175 _websocket . Connect ( ) ;
181176 }
182177
183178 public void Disconnect ( )
184179 {
185- var wait = new ManualResetEvent ( false ) ;
186- _heartbeatTimer . Dispose ( wait ) ;
187- wait . WaitOne ( ) ;
188-
189- _websocket . Close ( ) ;
190- _notifier . Close ( ) ;
180+ _heartbeatTimer . Change ( - 1 , - 1 ) ;
181+ _websocket . Close ( CloseStatusCode . Away ) ;
182+ _audioBox . Clear ( ) ;
183+ _id = null ;
184+ _name = null ;
191185 }
192186
193187 public void Write ( string message )
@@ -198,6 +192,9 @@ public void Write (string message)
198192 void IDisposable . Dispose ( )
199193 {
200194 Disconnect ( ) ;
195+
196+ _heartbeatTimer . Dispose ( ) ;
197+ _notifier . Close ( ) ;
201198 }
202199 }
203200}
0 commit comments