Supports RFC 6455
This commit is contained in:
27
Example1/AssemblyInfo.cs
Normal file
27
Example1/AssemblyInfo.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("Example1")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("")]
|
||||
[assembly: AssemblyCopyright("sta")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
|
318
Example1/AudioStreamer.cs
Normal file
318
Example1/AudioStreamer.cs
Normal file
@@ -0,0 +1,318 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
#if NOTIFY
|
||||
using Notifications;
|
||||
#endif
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using WebSocketSharp;
|
||||
using WebSocketSharp.Frame;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
public struct NfMessage
|
||||
{
|
||||
public string Summary;
|
||||
public string Body;
|
||||
public string Icon;
|
||||
}
|
||||
|
||||
public class AudioMessage
|
||||
{
|
||||
public uint user_id;
|
||||
public byte ch_num;
|
||||
public uint buffer_length;
|
||||
public float[,] buffer_array;
|
||||
}
|
||||
|
||||
public class TextMessage
|
||||
{
|
||||
public uint? user_id;
|
||||
public string name;
|
||||
public string type;
|
||||
public string message;
|
||||
}
|
||||
|
||||
public class ThreadState
|
||||
{
|
||||
public bool Enabled { get; set; }
|
||||
public AutoResetEvent Notification { get; private set; }
|
||||
|
||||
public ThreadState()
|
||||
{
|
||||
Enabled = true;
|
||||
Notification = new AutoResetEvent(false);
|
||||
}
|
||||
}
|
||||
|
||||
public class AudioStreamer : IDisposable
|
||||
{
|
||||
private Dictionary<uint, Queue> _audioBox;
|
||||
private Queue _msgQ;
|
||||
private string _name;
|
||||
private WaitCallback _notifyMsg;
|
||||
private ThreadState _notifyMsgState;
|
||||
private TimerCallback _sendHeartbeat;
|
||||
private Timer _heartbeatTimer;
|
||||
private uint? _user_id;
|
||||
private WebSocket _ws;
|
||||
|
||||
public AudioStreamer(string url)
|
||||
{
|
||||
_ws = new WebSocket(url);
|
||||
_msgQ = Queue.Synchronized(new Queue());
|
||||
_audioBox = new Dictionary<uint, Queue>();
|
||||
_user_id = null;
|
||||
|
||||
configure();
|
||||
}
|
||||
|
||||
private void configure()
|
||||
{
|
||||
_ws.OnOpen += (sender, e) =>
|
||||
{
|
||||
var msg = createTextMessage("connection", String.Empty);
|
||||
_ws.Send(msg);
|
||||
};
|
||||
|
||||
_ws.OnMessage += (sender, e) =>
|
||||
{
|
||||
switch (e.Type)
|
||||
{
|
||||
case Opcode.TEXT:
|
||||
var msg = parseTextMessage(e.Data);
|
||||
_msgQ.Enqueue(msg);
|
||||
break;
|
||||
case Opcode.BINARY:
|
||||
var audioMsg = parseAudioMessage(e.RawData);
|
||||
if (audioMsg.user_id == _user_id) goto default;
|
||||
if (_audioBox.ContainsKey(audioMsg.user_id))
|
||||
{
|
||||
_audioBox[audioMsg.user_id].Enqueue(audioMsg.buffer_array);
|
||||
}
|
||||
else
|
||||
{
|
||||
var q = Queue.Synchronized(new Queue());
|
||||
q.Enqueue(audioMsg.buffer_array);
|
||||
_audioBox.Add(audioMsg.user_id, q);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
_ws.OnError += (sender, e) =>
|
||||
{
|
||||
enNfMessage("[AudioStreamer] error", "WS Error: " + e.Data, "notification-message-im");
|
||||
};
|
||||
|
||||
_ws.OnClose += (sender, e) =>
|
||||
{
|
||||
enNfMessage
|
||||
(
|
||||
"[AudioStreamer] disconnect",
|
||||
String.Format("WS Close({0}:{1}): {2}", (ushort)e.Code, e.Code, e.Reason),
|
||||
"notification-message-im"
|
||||
);
|
||||
};
|
||||
|
||||
_notifyMsgState = new ThreadState();
|
||||
_notifyMsg = (state) =>
|
||||
{
|
||||
while (_notifyMsgState.Enabled)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
|
||||
if (_msgQ.Count > 0)
|
||||
{
|
||||
NfMessage msg = (NfMessage)_msgQ.Dequeue();
|
||||
#if NOTIFY
|
||||
Notification nf = new Notification(msg.Summary,
|
||||
msg.Body,
|
||||
msg.Icon);
|
||||
nf.AddHint("append", "allowed");
|
||||
nf.Show();
|
||||
#else
|
||||
Console.WriteLine("{0}: {1}", msg.Summary, msg.Body);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
_notifyMsgState.Notification.Set();
|
||||
};
|
||||
|
||||
_sendHeartbeat = (state) =>
|
||||
{
|
||||
var msg = createTextMessage("heartbeat", String.Empty);
|
||||
_ws.Send(msg);
|
||||
};
|
||||
}
|
||||
|
||||
private byte[] createAudioMessage(float[,] buffer_array)
|
||||
{
|
||||
List<byte> msg = new List<byte>();
|
||||
|
||||
uint user_id = (uint)_user_id;
|
||||
int ch_num = buffer_array.GetLength(0);
|
||||
int buffer_length = buffer_array.GetLength(1);
|
||||
|
||||
msg.AddRange(user_id.ToBytes(ByteOrder.BIG));
|
||||
msg.Add((byte)ch_num);
|
||||
msg.AddRange(((uint)buffer_length).ToBytes(ByteOrder.BIG));
|
||||
|
||||
ch_num.Times(i =>
|
||||
{
|
||||
buffer_length.Times(j =>
|
||||
{
|
||||
msg.AddRange(buffer_array[i, j].ToBytes(ByteOrder.BIG));
|
||||
});
|
||||
});
|
||||
|
||||
return msg.ToArray();
|
||||
}
|
||||
|
||||
private string createTextMessage(string type, string message)
|
||||
{
|
||||
var msg = new TextMessage
|
||||
{
|
||||
user_id = _user_id,
|
||||
name = _name,
|
||||
type = type,
|
||||
message = message
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(msg);
|
||||
}
|
||||
|
||||
private AudioMessage parseAudioMessage(byte[] data)
|
||||
{
|
||||
uint user_id = data.SubArray(0, 4).To<uint>(ByteOrder.BIG);
|
||||
byte ch_num = data.SubArray(4, 1)[0];
|
||||
uint buffer_length = data.SubArray(5, 4).To<uint>(ByteOrder.BIG);
|
||||
float[,] buffer_array = new float[ch_num, buffer_length];
|
||||
|
||||
int offset = 9;
|
||||
ch_num.Times(i =>
|
||||
{
|
||||
buffer_length.Times(j =>
|
||||
{
|
||||
buffer_array[i, j] = data.SubArray(offset, 4).To<float>(ByteOrder.BIG);
|
||||
offset += 4;
|
||||
});
|
||||
});
|
||||
|
||||
return new AudioMessage
|
||||
{
|
||||
user_id = user_id,
|
||||
ch_num = ch_num,
|
||||
buffer_length = buffer_length,
|
||||
buffer_array = buffer_array
|
||||
};
|
||||
}
|
||||
|
||||
private NfMessage parseTextMessage(string data)
|
||||
{
|
||||
JObject msg = JObject.Parse(data);
|
||||
uint user_id = (uint)msg["user_id"];
|
||||
string name = (string)msg["name"];
|
||||
string type = (string)msg["type"];
|
||||
|
||||
string message;
|
||||
switch (type)
|
||||
{
|
||||
case "connection":
|
||||
JArray users = (JArray)msg["message"];
|
||||
StringBuilder sb = new StringBuilder("Now keeping connection\n");
|
||||
foreach (JToken user in users)
|
||||
{
|
||||
sb.AppendFormat("user_id: {0} name: {1}\n", (uint)user["user_id"], (string)user["name"]);
|
||||
}
|
||||
message = sb.ToString().TrimEnd('\n');
|
||||
break;
|
||||
case "connected":
|
||||
_user_id = user_id;
|
||||
message = String.Format("user_id: {0} name: {1}", user_id, name);
|
||||
break;
|
||||
case "message":
|
||||
message = String.Format("{0}: {1}", name, (string)msg["message"]);
|
||||
break;
|
||||
case "start_music":
|
||||
message = String.Format("{0}: Started playing music!", name);
|
||||
break;
|
||||
default:
|
||||
message = "Received unknown type message: " + type;
|
||||
break;
|
||||
}
|
||||
|
||||
return new NfMessage
|
||||
{
|
||||
Summary = String.Format("[AudioStreamer] {0}", type),
|
||||
Body = message,
|
||||
Icon = "notification-message-im"
|
||||
};
|
||||
}
|
||||
|
||||
private void enNfMessage(string summary, string body, string icon)
|
||||
{
|
||||
var msg = new NfMessage
|
||||
{
|
||||
Summary = summary,
|
||||
Body = body,
|
||||
Icon = icon
|
||||
};
|
||||
|
||||
_msgQ.Enqueue(msg);
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
string name;
|
||||
do
|
||||
{
|
||||
Console.Write("Your name > ");
|
||||
name = Console.ReadLine();
|
||||
}
|
||||
while (name == String.Empty);
|
||||
|
||||
_name = name;
|
||||
|
||||
_ws.Connect();
|
||||
|
||||
ThreadPool.QueueUserWorkItem(_notifyMsg);
|
||||
_heartbeatTimer = new Timer(_sendHeartbeat, null, 30 * 1000, 30 * 1000);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
var wait = new AutoResetEvent(false);
|
||||
_heartbeatTimer.Dispose(wait);
|
||||
wait.WaitOne();
|
||||
|
||||
_ws.Close();
|
||||
|
||||
_notifyMsgState.Enabled = false;
|
||||
_notifyMsgState.Notification.WaitOne();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
public void Write(string data)
|
||||
{
|
||||
var msg = createTextMessage("message", data);
|
||||
_ws.Send(msg);
|
||||
}
|
||||
|
||||
public void Write(FileInfo file)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
73
Example1/Example1.csproj
Normal file
73
Example1/Example1.csproj
Normal file
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.21022</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{390E2568-57B7-4D17-91E5-C29336368CCF}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>Example</RootNamespace>
|
||||
<AssemblyName>example1</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug</OutputPath>
|
||||
<DefineConstants>DEBUG;</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Release</OutputPath>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Ubuntu|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug_Ubuntu</OutputPath>
|
||||
<DefineConstants>DEBUG;NOTIFY</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release_Ubuntu|AnyCPU' ">
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Release_Ubuntu</OutputPath>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Externalconsole>true</Externalconsole>
|
||||
<DefineConstants>NOTIFY</DefineConstants>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="notify-sharp, Version=0.4.0.0, Culture=neutral, PublicKeyToken=2df29c54e245917a">
|
||||
<Private>False</Private>
|
||||
<Package>notify-sharp</Package>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AssemblyInfo.cs" />
|
||||
<Compile Include="AudioStreamer.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\websocket-sharp\websocket-sharp.csproj">
|
||||
<Project>{B357BAC7-529E-4D81-A0D2-71041B19C8DE}</Project>
|
||||
<Name>websocket-sharp</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
</Project>
|
35
Example1/Program.cs
Normal file
35
Example1/Program.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Example
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
//using (AudioStreamer streamer = new AudioStreamer("ws://localhost:3000/socket"))
|
||||
using (AudioStreamer streamer = new AudioStreamer("ws://agektmr.node-ninja.com:3000/socket"))
|
||||
{
|
||||
streamer.Connect();
|
||||
|
||||
Thread.Sleep(500);
|
||||
Console.WriteLine("\nType \"exit\" to exit.\n");
|
||||
|
||||
string data;
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
|
||||
Console.Write("> ");
|
||||
data = Console.ReadLine();
|
||||
if (data == "exit")
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
streamer.Write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user