[Modify] Polish it
This commit is contained in:
parent
a870f7389f
commit
2da22cacf2
@ -12,10 +12,10 @@ namespace Example1
|
|||||||
internal class AudioStreamer : IDisposable
|
internal class AudioStreamer : IDisposable
|
||||||
{
|
{
|
||||||
private Dictionary<uint, Queue> _audioBox;
|
private Dictionary<uint, Queue> _audioBox;
|
||||||
private Timer _heartbeatTimer;
|
|
||||||
private uint? _id;
|
private uint? _id;
|
||||||
private string _name;
|
private string _name;
|
||||||
private Notifier _notifier;
|
private Notifier _notifier;
|
||||||
|
private Timer _timer;
|
||||||
private WebSocket _websocket;
|
private WebSocket _websocket;
|
||||||
|
|
||||||
public AudioStreamer (string url)
|
public AudioStreamer (string url)
|
||||||
@ -23,9 +23,9 @@ namespace Example1
|
|||||||
_websocket = new WebSocket (url);
|
_websocket = new WebSocket (url);
|
||||||
|
|
||||||
_audioBox = new Dictionary<uint, Queue> ();
|
_audioBox = new Dictionary<uint, Queue> ();
|
||||||
_heartbeatTimer = new Timer (sendHeartbeat, null, -1, -1);
|
|
||||||
_id = null;
|
_id = null;
|
||||||
_notifier = new Notifier ();
|
_notifier = new Notifier ();
|
||||||
|
_timer = new Timer (sendHeartbeat, null, -1, -1);
|
||||||
|
|
||||||
configure ();
|
configure ();
|
||||||
}
|
}
|
||||||
@ -40,10 +40,12 @@ namespace Example1
|
|||||||
|
|
||||||
_websocket.OnMessage += (sender, e) => {
|
_websocket.OnMessage += (sender, e) => {
|
||||||
if (e.IsText) {
|
if (e.IsText) {
|
||||||
_notifier.Notify (convertTextMessage (e.Data));
|
_notifier.Notify (convertToNotificationMessage (e.Data));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
var msg = convertBinaryMessage (e.RawData);
|
if (e.IsBinary) {
|
||||||
|
var msg = convertToAudioMessage (e.RawData);
|
||||||
if (msg.user_id == _id)
|
if (msg.user_id == _id)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -55,6 +57,8 @@ namespace Example1
|
|||||||
var queue = Queue.Synchronized (new Queue ());
|
var queue = Queue.Synchronized (new Queue ());
|
||||||
queue.Enqueue (msg.buffer_array);
|
queue.Enqueue (msg.buffer_array);
|
||||||
_audioBox.Add (msg.user_id, queue);
|
_audioBox.Add (msg.user_id, queue);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,7 +68,8 @@ namespace Example1
|
|||||||
Summary = "AudioStreamer (error)",
|
Summary = "AudioStreamer (error)",
|
||||||
Body = e.Message,
|
Body = e.Message,
|
||||||
Icon = "notification-message-im"
|
Icon = "notification-message-im"
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
_websocket.OnClose += (sender, e) =>
|
_websocket.OnClose += (sender, e) =>
|
||||||
_notifier.Notify (
|
_notifier.Notify (
|
||||||
@ -72,23 +77,27 @@ namespace Example1
|
|||||||
Summary = "AudioStreamer (disconnect)",
|
Summary = "AudioStreamer (disconnect)",
|
||||||
Body = String.Format ("code: {0} reason: {1}", e.Code, e.Reason),
|
Body = String.Format ("code: {0} reason: {1}", e.Code, e.Reason),
|
||||||
Icon = "notification-message-im"
|
Icon = "notification-message-im"
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private AudioMessage convertBinaryMessage (byte[] data)
|
private AudioMessage convertToAudioMessage (byte[] binaryMessage)
|
||||||
{
|
{
|
||||||
var id = data.SubArray (0, 4).To<uint> (ByteOrder.Big);
|
var id = binaryMessage.SubArray (0, 4).To<uint> (ByteOrder.Big);
|
||||||
var chNum = data.SubArray (4, 1)[0];
|
var chNum = binaryMessage.SubArray (4, 1)[0];
|
||||||
var buffLen = data.SubArray (5, 4).To<uint> (ByteOrder.Big);
|
var buffLen = binaryMessage.SubArray (5, 4).To<uint> (ByteOrder.Big);
|
||||||
var buffArr = new float[chNum, buffLen];
|
var buffArr = new float[chNum, buffLen];
|
||||||
|
|
||||||
var offset = 9;
|
var offset = 9;
|
||||||
((int) chNum).Times (
|
((int) chNum).Times (
|
||||||
i => buffLen.Times (
|
i =>
|
||||||
|
buffLen.Times (
|
||||||
j => {
|
j => {
|
||||||
buffArr[i, j] = data.SubArray (offset, 4).To<float> (ByteOrder.Big);
|
buffArr[i, j] = binaryMessage.SubArray (offset, 4).To<float> (ByteOrder.Big);
|
||||||
offset += 4;
|
offset += 4;
|
||||||
}));
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
return new AudioMessage {
|
return new AudioMessage {
|
||||||
user_id = id,
|
user_id = id,
|
||||||
@ -98,9 +107,9 @@ namespace Example1
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private NotificationMessage convertTextMessage (string data)
|
private NotificationMessage convertToNotificationMessage (string textMessage)
|
||||||
{
|
{
|
||||||
var json = JObject.Parse (data);
|
var json = JObject.Parse (textMessage);
|
||||||
var id = (uint) json["user_id"];
|
var id = (uint) json["user_id"];
|
||||||
var name = (string) json["name"];
|
var name = (string) json["name"];
|
||||||
var type = (string) json["type"];
|
var type = (string) json["type"];
|
||||||
@ -115,15 +124,17 @@ namespace Example1
|
|||||||
else if (type == "connection") {
|
else if (type == "connection") {
|
||||||
var users = (JArray) json["message"];
|
var users = (JArray) json["message"];
|
||||||
var buff = new StringBuilder ("Now keeping connections:");
|
var buff = new StringBuilder ("Now keeping connections:");
|
||||||
foreach (JToken user in users)
|
foreach (JToken user in users) {
|
||||||
buff.AppendFormat (
|
buff.AppendFormat (
|
||||||
"\n- user_id: {0} name: {1}", (uint) user["user_id"], (string) user["name"]);
|
"\n- user_id: {0} name: {1}", (uint) user["user_id"], (string) user["name"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
body = buff.ToString ();
|
body = buff.ToString ();
|
||||||
}
|
}
|
||||||
else if (type == "connected") {
|
else if (type == "connected") {
|
||||||
_id = id;
|
_id = id;
|
||||||
_heartbeatTimer.Change (30000, 30000);
|
_timer.Change (30000, 30000);
|
||||||
body = String.Format ("user_id: {0} name: {1}", id, name);
|
body = String.Format ("user_id: {0} name: {1}", id, name);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -150,8 +161,11 @@ namespace Example1
|
|||||||
msg.AddRange (((uint) buffLen).ToByteArray (ByteOrder.Big));
|
msg.AddRange (((uint) buffLen).ToByteArray (ByteOrder.Big));
|
||||||
|
|
||||||
chNum.Times (
|
chNum.Times (
|
||||||
i => buffLen.Times (
|
i =>
|
||||||
j => msg.AddRange (bufferArray[i, j].ToByteArray (ByteOrder.Big))));
|
buffLen.Times (
|
||||||
|
j => msg.AddRange (bufferArray[i, j].ToByteArray (ByteOrder.Big))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
return msg.ToArray ();
|
return msg.ToArray ();
|
||||||
}
|
}
|
||||||
@ -164,7 +178,8 @@ namespace Example1
|
|||||||
name = _name,
|
name = _name,
|
||||||
type = type,
|
type = type,
|
||||||
message = message
|
message = message
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendHeartbeat (object state)
|
private void sendHeartbeat (object state)
|
||||||
@ -180,7 +195,7 @@ namespace Example1
|
|||||||
|
|
||||||
public void Disconnect ()
|
public void Disconnect ()
|
||||||
{
|
{
|
||||||
_heartbeatTimer.Change (-1, -1);
|
_timer.Change (-1, -1);
|
||||||
_websocket.Close (CloseStatusCode.Away);
|
_websocket.Close (CloseStatusCode.Away);
|
||||||
_audioBox.Clear ();
|
_audioBox.Clear ();
|
||||||
_id = null;
|
_id = null;
|
||||||
@ -195,8 +210,7 @@ namespace Example1
|
|||||||
void IDisposable.Dispose ()
|
void IDisposable.Dispose ()
|
||||||
{
|
{
|
||||||
Disconnect ();
|
Disconnect ();
|
||||||
|
_timer.Dispose ();
|
||||||
_heartbeatTimer.Dispose ();
|
|
||||||
_notifier.Close ();
|
_notifier.Close ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user