@@ -31,18 +31,18 @@ public AudioStreamer (string url)
31
31
configure ( ) ;
32
32
}
33
33
34
- private AudioMessage acceptBinaryMessage ( byte [ ] value )
34
+ private AudioMessage acceptBinaryMessage ( byte [ ] data )
35
35
{
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 ) ;
39
39
var bufferArray = new float [ chNum , bufferLength ] ;
40
40
41
41
var offset = 9 ;
42
42
( ( int ) chNum ) . Times (
43
43
i => bufferLength . Times (
44
44
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 ) ;
46
46
offset += 4 ;
47
47
} ) ) ;
48
48
@@ -54,37 +54,37 @@ private AudioMessage acceptBinaryMessage (byte [] value)
54
54
} ;
55
55
}
56
56
57
- private NotificationMessage acceptTextMessage ( string value )
57
+ private NotificationMessage acceptTextMessage ( string data )
58
58
{
59
- var json = JObject . Parse ( value ) ;
59
+ var json = JObject . Parse ( data ) ;
60
60
var id = ( uint ) json [ "user_id" ] ;
61
61
var name = ( string ) json [ "name" ] ;
62
62
var type = ( string ) json [ "type" ] ;
63
63
64
64
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" ) {
66
70
var users = ( JArray ) json [ "message" ] ;
67
- var msg = new StringBuilder ( "Now keeping connection :" ) ;
71
+ var msg = new StringBuilder ( "Now keeping connections :" ) ;
68
72
foreach ( JToken user in users )
69
73
msg . AppendFormat (
70
74
"\n - user_id: {0} name: {1}" , ( uint ) user [ "user_id" ] , ( string ) user [ "name" ] ) ;
71
75
72
76
message = msg . ToString ( ) ;
73
77
}
74
78
else if ( type == "connected" ) {
75
- _heartbeatTimer . Change ( 30000 , 30000 ) ;
76
79
_id = id ;
80
+ _heartbeatTimer . Change ( 30000 , 30000 ) ;
77
81
message = String . Format ( "user_id: {0} name: {1}" , id , name ) ;
78
82
}
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 ) ;
83
83
else
84
84
message = "Received unknown type message." ;
85
85
86
86
return new NotificationMessage {
87
- Summary = String . Format ( "AudioStreamer Message ({0})" , type ) ,
87
+ Summary = String . Format ( "AudioStreamer ({0})" , type ) ,
88
88
Body = message ,
89
89
Icon = "notification-message-im"
90
90
} ;
@@ -120,16 +120,16 @@ private void configure ()
120
120
_websocket . OnError += ( sender , e ) =>
121
121
_notifier . Notify (
122
122
new NotificationMessage {
123
- Summary = "AudioStreamer Error " ,
123
+ Summary = "AudioStreamer (error) " ,
124
124
Body = e . Message ,
125
125
Icon = "notification-message-im"
126
126
} ) ;
127
127
128
128
_websocket . OnClose += ( sender , e ) =>
129
129
_notifier . Notify (
130
130
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 ) ,
133
133
Icon = "notification-message-im"
134
134
} ) ;
135
135
}
@@ -169,25 +169,19 @@ private void sendHeartbeat (object state)
169
169
_websocket . Send ( createTextMessage ( "heartbeat" , String . Empty ) ) ;
170
170
}
171
171
172
- public void Connect ( )
172
+ public void Connect ( string username )
173
173
{
174
- do {
175
- Console . Write ( "Input your name> " ) ;
176
- _name = Console . ReadLine ( ) ;
177
- }
178
- while ( _name . Length == 0 ) ;
179
-
174
+ _name = username ;
180
175
_websocket . Connect ( ) ;
181
176
}
182
177
183
178
public void Disconnect ( )
184
179
{
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 ;
191
185
}
192
186
193
187
public void Write ( string message )
@@ -198,6 +192,9 @@ public void Write (string message)
198
192
void IDisposable . Dispose ( )
199
193
{
200
194
Disconnect ( ) ;
195
+
196
+ _heartbeatTimer . Dispose ( ) ;
197
+ _notifier . Close ( ) ;
201
198
}
202
199
}
203
200
}
0 commit comments