Browse Source

Modified a few for Example1

pull/80/head
sta 11 years ago
parent
commit
0fc7c82e70
  1. 8
      Example1/AudioMessage.cs
  2. 141
      Example1/AudioStreamer.cs
  3. 12
      Example1/Notifier.cs
  4. 2
      Example1/Program.cs

8
Example1/AudioMessage.cs

@ -4,9 +4,9 @@ namespace Example1
{ {
internal class AudioMessage internal class AudioMessage
{ {
public uint user_id;
public byte ch_num;
public uint buffer_length;
public float [,] buffer_array;
public uint user_id;
public byte ch_num;
public uint buffer_length;
public float[,] buffer_array;
} }
} }

141
Example1/AudioStreamer.cs

@ -3,7 +3,6 @@ using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using WebSocketSharp; using WebSocketSharp;
@ -31,65 +30,6 @@ namespace Example1
configure (); configure ();
} }
private AudioMessage acceptBinaryMessage (byte [] data)
{
var id = data.SubArray (0, 4).To<uint> (ByteOrder.Big);
var chNum = data.SubArray (4, 1) [0];
var bufferLength = data.SubArray (5, 4).To<uint> (ByteOrder.Big);
var bufferArray = new float [chNum, bufferLength];
var offset = 9;
((int) chNum).Times (
i => bufferLength.Times (
j => {
bufferArray [i, j] = data.SubArray (offset, 4).To<float> (ByteOrder.Big);
offset += 4;
}));
return new AudioMessage {
user_id = id,
ch_num = chNum,
buffer_length = bufferLength,
buffer_array = bufferArray
};
}
private NotificationMessage acceptTextMessage (string data)
{
var json = JObject.Parse (data);
var id = (uint) json ["user_id"];
var name = (string) json ["name"];
var type = (string) json ["type"];
string message;
if (type == "message")
message = String.Format ("{0}: {1}", name, (string) json ["message"]);
else if (type == "start_music")
message = String.Format ("{0}: Started playing music!", name);
else if (type == "connection") {
var users = (JArray) json ["message"];
var msg = new StringBuilder ("Now keeping connections:");
foreach (JToken user in users)
msg.AppendFormat (
"\n- user_id: {0} name: {1}", (uint) user ["user_id"], (string) user ["name"]);
message = msg.ToString ();
}
else if (type == "connected") {
_id = id;
_heartbeatTimer.Change (30000, 30000);
message = String.Format ("user_id: {0} name: {1}", id, name);
}
else
message = "Received unknown type message.";
return new NotificationMessage {
Summary = String.Format ("AudioStreamer ({0})", type),
Body = message,
Icon = "notification-message-im"
};
}
private void configure () private void configure ()
{ {
#if DEBUG #if DEBUG
@ -99,15 +39,16 @@ namespace Example1
_websocket.Send (createTextMessage ("connection", String.Empty)); _websocket.Send (createTextMessage ("connection", String.Empty));
_websocket.OnMessage += (sender, e) => { _websocket.OnMessage += (sender, e) => {
if (e.Type == Opcode.Text)
_notifier.Notify (acceptTextMessage (e.Data));
if (e.Type == Opcode.Text) {
_notifier.Notify (convertTextMessage (e.Data));
}
else { else {
var msg = acceptBinaryMessage (e.RawData);
var msg = convertBinaryMessage (e.RawData);
if (msg.user_id == _id) if (msg.user_id == _id)
return; return;
if (_audioBox.ContainsKey (msg.user_id)) { if (_audioBox.ContainsKey (msg.user_id)) {
_audioBox [msg.user_id].Enqueue (msg.buffer_array);
_audioBox[msg.user_id].Enqueue (msg.buffer_array);
return; return;
} }
@ -134,21 +75,83 @@ namespace Example1
}); });
} }
private byte [] createAudioMessage (float [,] bufferArray)
private AudioMessage convertBinaryMessage (byte[] data)
{
var id = data.SubArray (0, 4).To<uint> (ByteOrder.Big);
var chNum = data.SubArray (4, 1)[0];
var buffLen = data.SubArray (5, 4).To<uint> (ByteOrder.Big);
var buffArr = new float[chNum, buffLen];
var offset = 9;
((int) chNum).Times (
i => buffLen.Times (
j => {
buffArr[i, j] = data.SubArray (offset, 4).To<float> (ByteOrder.Big);
offset += 4;
}));
return new AudioMessage {
user_id = id,
ch_num = chNum,
buffer_length = buffLen,
buffer_array = buffArr
};
}
private NotificationMessage convertTextMessage (string data)
{
var json = JObject.Parse (data);
var id = (uint) json["user_id"];
var name = (string) json["name"];
var type = (string) json["type"];
string body;
if (type == "message") {
body = String.Format ("{0}: {1}", name, (string) json["message"]);
}
else if (type == "start_music") {
body = String.Format ("{0}: Started playing music!", name);
}
else if (type == "connection") {
var users = (JArray) json["message"];
var buff = new StringBuilder ("Now keeping connections:");
foreach (JToken user in users)
buff.AppendFormat (
"\n- user_id: {0} name: {1}", (uint) user["user_id"], (string) user["name"]);
body = buff.ToString ();
}
else if (type == "connected") {
_id = id;
_heartbeatTimer.Change (30000, 30000);
body = String.Format ("user_id: {0} name: {1}", id, name);
}
else {
body = "Received unknown type message.";
}
return new NotificationMessage {
Summary = String.Format ("AudioStreamer ({0})", type),
Body = body,
Icon = "notification-message-im"
};
}
private byte[] createBinaryMessage (float[,] bufferArray)
{ {
var msg = new List<byte> (); var msg = new List<byte> ();
var id = (uint) _id; var id = (uint) _id;
var chNum = bufferArray.GetLength (0); var chNum = bufferArray.GetLength (0);
var bufferLength = bufferArray.GetLength (1);
var buffLen = bufferArray.GetLength (1);
msg.AddRange (id.ToByteArray (ByteOrder.Big)); msg.AddRange (id.ToByteArray (ByteOrder.Big));
msg.Add ((byte) chNum); msg.Add ((byte) chNum);
msg.AddRange (((uint) bufferLength).ToByteArray (ByteOrder.Big));
msg.AddRange (((uint) buffLen).ToByteArray (ByteOrder.Big));
chNum.Times ( chNum.Times (
i => bufferLength.Times (
j => msg.AddRange (bufferArray [i, j].ToByteArray (ByteOrder.Big))));
i => buffLen.Times (
j => msg.AddRange (bufferArray[i, j].ToByteArray (ByteOrder.Big))));
return msg.ToArray (); return msg.ToArray ();
} }

12
Example1/Notifier.cs

@ -35,8 +35,9 @@ namespace Example1
Console.WriteLine (msg); Console.WriteLine (msg);
#endif #endif
} }
else
else {
Thread.Sleep (500); Thread.Sleep (500);
}
} }
_waitHandle.Set (); _waitHandle.Set ();
@ -45,19 +46,17 @@ namespace Example1
public int Count { public int Count {
get { get {
lock (_sync) {
lock (_sync)
return _queue.Count; return _queue.Count;
}
} }
} }
private NotificationMessage dequeue () private NotificationMessage dequeue ()
{ {
lock (_sync) {
lock (_sync)
return _queue.Count > 0 return _queue.Count > 0
? _queue.Dequeue () ? _queue.Dequeue ()
: null; : null;
}
} }
public void Close () public void Close ()
@ -69,10 +68,9 @@ namespace Example1
public void Notify (NotificationMessage message) public void Notify (NotificationMessage message)
{ {
lock (_sync) {
lock (_sync)
if (_enabled) if (_enabled)
_queue.Enqueue (message); _queue.Enqueue (message);
}
} }
void IDisposable.Dispose () void IDisposable.Dispose ()

2
Example1/Program.cs

@ -5,7 +5,7 @@ namespace Example1
{ {
public class Program public class Program
{ {
public static void Main (string [] args)
public static void Main (string[] args)
{ {
using (var streamer = new AudioStreamer ("ws://agektmr.node-ninja.com:3000/socket")) using (var streamer = new AudioStreamer ("ws://agektmr.node-ninja.com:3000/socket"))
//using (var streamer = new AudioStreamer ("ws://localhost:3000/socket")) //using (var streamer = new AudioStreamer ("ws://localhost:3000/socket"))

Loading…
Cancel
Save