Skip to content

Commit 20e2ac2

Browse files
committed
Fix a few
1 parent f068425 commit 20e2ac2

File tree

6 files changed

+43
-47
lines changed

6 files changed

+43
-47
lines changed

Example/AssemblyInfo.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[assembly: AssemblyConfiguration("")]
1010
[assembly: AssemblyCompany("")]
1111
[assembly: AssemblyProduct("")]
12-
[assembly: AssemblyCopyright("sta")]
12+
[assembly: AssemblyCopyright("sta.blockhead")]
1313
[assembly: AssemblyTrademark("")]
1414
[assembly: AssemblyCulture("")]
1515

@@ -24,4 +24,3 @@
2424

2525
//[assembly: AssemblyDelaySign(false)]
2626
//[assembly: AssemblyKeyFile("")]
27-

Example/NotificationMessage.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ namespace Example
44
{
55
internal class NotificationMessage
66
{
7-
public NotificationMessage ()
8-
{
9-
}
10-
117
public string Body {
128
get; set;
139
}
@@ -22,7 +18,7 @@ public string Summary {
2218

2319
public override string ToString ()
2420
{
25-
return String.Format ("[{0}: {1}]", Summary, Body);
21+
return String.Format ("{0}: {1}", Summary, Body);
2622
}
2723
}
2824
}

Example/Program.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ public static void Main (string [] args)
2626

2727
ws.OnMessage += (sender, e) =>
2828
nf.Notify (
29-
new NotificationMessage () {
29+
new NotificationMessage {
3030
Summary = "WebSocket Message",
3131
Body = e.Data,
3232
Icon = "notification-message-im"
3333
});
3434

3535
ws.OnError += (sender, e) =>
3636
nf.Notify (
37-
new NotificationMessage () {
37+
new NotificationMessage {
3838
Summary = "WebSocket Error",
3939
Body = e.Message,
4040
Icon = "notification-message-im"
4141
});
4242

4343
ws.OnClose += (sender, e) =>
4444
nf.Notify (
45-
new NotificationMessage () {
45+
new NotificationMessage {
4646
Summary = String.Format ("WebSocket Close ({0})", e.Code),
4747
Body = e.Reason,
4848
Icon = "notification-message-im"
@@ -51,7 +51,6 @@ public static void Main (string [] args)
5151
#if DEBUG
5252
ws.Log.Level = LogLevel.Trace;
5353
#endif
54-
5554
// Per-message Compression
5655
//ws.Compression = CompressionMethod.Deflate;
5756

@@ -80,9 +79,8 @@ public static void Main (string [] args)
8079
Thread.Sleep (500);
8180
Console.Write ("> ");
8281
var msg = Console.ReadLine ();
83-
if (msg == "exit") {
82+
if (msg == "exit")
8483
break;
85-
}
8684

8785
ws.Send (msg);
8886
}

Example1/AssemblyInfo.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[assembly: AssemblyConfiguration("")]
1010
[assembly: AssemblyCompany("")]
1111
[assembly: AssemblyProduct("")]
12-
[assembly: AssemblyCopyright("sta")]
12+
[assembly: AssemblyCopyright("sta.blockhead")]
1313
[assembly: AssemblyTrademark("")]
1414
[assembly: AssemblyCulture("")]
1515

@@ -24,4 +24,3 @@
2424

2525
//[assembly: AssemblyDelaySign(false)]
2626
//[assembly: AssemblyKeyFile("")]
27-

Example1/AudioStreamer.cs

+28-31
Original file line numberDiff line numberDiff line change
@@ -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
}

Example1/Program.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ public static void Main (string [] args)
1010
using (var streamer = new AudioStreamer ("ws://agektmr.node-ninja.com:3000/socket"))
1111
//using (var streamer = new AudioStreamer ("ws://localhost:3000/socket"))
1212
{
13-
streamer.Connect ();
13+
string name;
14+
do {
15+
Console.Write ("Input your name> ");
16+
name = Console.ReadLine ();
17+
}
18+
while (name.Length == 0);
19+
20+
streamer.Connect (name);
1421

1522
Console.WriteLine ("\nType \"exit\" to exit.\n");
1623
while (true) {

0 commit comments

Comments
 (0)