[Delete] Example1

This commit is contained in:
sta 2022-02-02 22:06:03 +09:00
parent 1c9fa6b2cf
commit a845aa1230
9 changed files with 0 additions and 554 deletions

View File

@ -1,26 +0,0 @@
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.blockhead")]
[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("")]

View File

@ -1,192 +0,0 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using WebSocketSharp;
namespace Example1
{
internal class AudioStreamer : IDisposable
{
private Dictionary<uint, Queue> _audioBox;
private uint? _id;
private string _name;
private Notifier _notifier;
private Timer _timer;
private WebSocket _websocket;
public AudioStreamer (string url)
{
_websocket = new WebSocket (url);
_audioBox = new Dictionary<uint, Queue> ();
_id = null;
_notifier = new Notifier ();
_timer = new Timer (sendHeartbeat, null, -1, -1);
configure ();
}
private void configure ()
{
#if DEBUG
_websocket.Log.Level = LogLevel.Trace;
#endif
_websocket.OnOpen += (sender, e) =>
_websocket.Send (createTextMessage ("connection", String.Empty));
_websocket.OnMessage += (sender, e) => {
if (e.IsText) {
_notifier.Notify (processTextMessage (e.Data));
return;
}
if (e.IsBinary) {
processBinaryMessage (e.RawData);
return;
}
};
_websocket.OnError += (sender, e) =>
_notifier.Notify (
new NotificationMessage {
Summary = "AudioStreamer (error)",
Body = e.Message,
Icon = "notification-message-im"
}
);
_websocket.OnClose += (sender, e) =>
_notifier.Notify (
new NotificationMessage {
Summary = "AudioStreamer (disconnect)",
Body = String.Format ("code: {0} reason: {1}", e.Code, e.Reason),
Icon = "notification-message-im"
}
);
}
private byte[] createBinaryMessage (float[,] bufferArray)
{
return new BinaryMessage {
UserID = (uint) _id,
ChannelNumber = (byte) bufferArray.GetLength (0),
BufferLength = (uint) bufferArray.GetLength (1),
BufferArray = bufferArray
}
.ToArray ();
}
private string createTextMessage (string type, string message)
{
return new TextMessage {
UserID = _id,
Name = _name,
Type = type,
Message = message
}
.ToString ();
}
private void processBinaryMessage (byte[] data)
{
var msg = BinaryMessage.Parse (data);
var id = msg.UserID;
if (id == _id)
return;
Queue queue;
if (_audioBox.TryGetValue (id, out queue)) {
queue.Enqueue (msg.BufferArray);
return;
}
queue = Queue.Synchronized (new Queue ());
queue.Enqueue (msg.BufferArray);
_audioBox.Add (id, queue);
}
private NotificationMessage processTextMessage (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;
_timer.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 void sendHeartbeat (object state)
{
_websocket.Send (createTextMessage ("heartbeat", String.Empty));
}
public void Close ()
{
Disconnect ();
_timer.Dispose ();
_notifier.Close ();
}
public void Connect (string username)
{
_name = username;
_websocket.Connect ();
}
public void Disconnect ()
{
_timer.Change (-1, -1);
_websocket.Close (CloseStatusCode.Away);
_audioBox.Clear ();
_id = null;
_name = null;
}
public void Write (string message)
{
_websocket.Send (createTextMessage ("message", message));
}
void IDisposable.Dispose ()
{
Close ();
}
}
}

View File

@ -1,74 +0,0 @@
using System;
using System.Collections.Generic;
using WebSocketSharp;
namespace Example1
{
internal class BinaryMessage
{
public uint UserID {
get; set;
}
public byte ChannelNumber {
get; set;
}
public uint BufferLength {
get; set;
}
public float[,] BufferArray {
get; set;
}
public static BinaryMessage Parse (byte[] data)
{
var id = data.SubArray (0, 4).To<uint> (ByteOrder.Big);
var num = data.SubArray (4, 1)[0];
var len = data.SubArray (5, 4).To<uint> (ByteOrder.Big);
var arr = new float[num, len];
var offset = 9;
((uint) num).Times (
i =>
len.Times (
j => {
arr[i, j] = data.SubArray (offset, 4).To<float> (ByteOrder.Big);
offset += 4;
}
)
);
return new BinaryMessage {
UserID = id,
ChannelNumber = num,
BufferLength = len,
BufferArray = arr
};
}
public byte[] ToArray ()
{
var buff = new List<byte> ();
var id = UserID;
var num = ChannelNumber;
var len = BufferLength;
var arr = BufferArray;
buff.AddRange (id.ToByteArray (ByteOrder.Big));
buff.Add (num);
buff.AddRange (len.ToByteArray (ByteOrder.Big));
((uint) num).Times (
i =>
len.Times (
j => buff.AddRange (arr[i, j].ToByteArray (ByteOrder.Big))
)
);
return buff.ToArray ();
}
}
}

View File

@ -1,77 +0,0 @@
<?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;UBUNTU</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>UBUNTU</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" />
<Compile Include="TextMessage.cs" />
<Compile Include="NotificationMessage.cs" />
<Compile Include="Notifier.cs" />
<Compile Include="BinaryMessage.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>

View File

@ -1,24 +0,0 @@
using System;
namespace Example1
{
internal class NotificationMessage
{
public string Body {
get; set;
}
public string Icon {
get; set;
}
public string Summary {
get; set;
}
public override string ToString ()
{
return String.Format ("{0}: {1}", Summary, Body);
}
}
}

View File

@ -1,81 +0,0 @@
#if UBUNTU
using Notifications;
#endif
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace Example1
{
internal class Notifier : IDisposable
{
private volatile bool _enabled;
private ManualResetEvent _exited;
private Queue<NotificationMessage> _queue;
private object _sync;
public Notifier ()
{
_enabled = true;
_exited = new ManualResetEvent (false);
_queue = new Queue<NotificationMessage> ();
_sync = ((ICollection) _queue).SyncRoot;
ThreadPool.QueueUserWorkItem (
state => {
while (_enabled || Count > 0) {
var msg = dequeue ();
if (msg != null) {
#if UBUNTU
var nf = new Notification (msg.Summary, msg.Body, msg.Icon);
nf.AddHint ("append", "allowed");
nf.Show ();
#else
Console.WriteLine (msg);
#endif
}
else {
Thread.Sleep (500);
}
}
_exited.Set ();
}
);
}
public int Count {
get {
lock (_sync)
return _queue.Count;
}
}
private NotificationMessage dequeue ()
{
lock (_sync)
return _queue.Count > 0 ? _queue.Dequeue () : null;
}
public void Close ()
{
_enabled = false;
_exited.WaitOne ();
_exited.Close ();
}
public void Notify (NotificationMessage message)
{
lock (_sync) {
if (_enabled)
_queue.Enqueue (message);
}
}
void IDisposable.Dispose ()
{
Close ();
}
}
}

View File

@ -1,37 +0,0 @@
using System;
using System.Threading;
namespace Example1
{
public class Program
{
public static void Main (string[] args)
{
// The AudioStreamer class provides a client (chat) for AudioStreamer
// (https://github.com/agektmr/AudioStreamer).
using (var streamer = new AudioStreamer ("ws://localhost:3000/socket"))
{
string name;
do {
Console.Write ("Input your name> ");
name = Console.ReadLine ();
}
while (name.Length == 0);
streamer.Connect (name);
Console.WriteLine ("\nType 'exit' to exit.\n");
while (true) {
Thread.Sleep (1000);
Console.Write ("> ");
var msg = Console.ReadLine ();
if (msg == "exit")
break;
streamer.Write (msg);
}
}
}
}
}

View File

@ -1,33 +0,0 @@
using Newtonsoft.Json;
using System;
namespace Example1
{
internal class TextMessage
{
[JsonProperty ("user_id")]
public uint? UserID {
get; set;
}
[JsonProperty ("name")]
public string Name {
get; set;
}
[JsonProperty ("type")]
public string Type {
get; set;
}
[JsonProperty ("message")]
public string Message {
get; set;
}
public override string ToString ()
{
return JsonConvert.SerializeObject (this);
}
}
}

View File

@ -5,8 +5,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "websocket-sharp", "websocke
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{52805AEC-EFB1-4F42-BB8E-3ED4E692C568}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example1", "Example1\Example1.csproj", "{390E2568-57B7-4D17-91E5-C29336368CCF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example2", "Example2\Example2.csproj", "{B81A24C8-25BB-42B2-AF99-1E1EACCE74C7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example3", "Example3\Example3.csproj", "{C648BA25-77E5-4A40-A97F-D0AA37B9FB26}"
@ -19,14 +17,6 @@ Global
Release_Ubuntu|Any CPU = Release_Ubuntu|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{390E2568-57B7-4D17-91E5-C29336368CCF}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Release_Ubuntu|Any CPU.ActiveCfg = Release_Ubuntu|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Release_Ubuntu|Any CPU.Build.0 = Release_Ubuntu|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{390E2568-57B7-4D17-91E5-C29336368CCF}.Release|Any CPU.Build.0 = Release|Any CPU
{52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug_Ubuntu|Any CPU.ActiveCfg = Debug_Ubuntu|Any CPU
{52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug_Ubuntu|Any CPU.Build.0 = Debug_Ubuntu|Any CPU
{52805AEC-EFB1-4F42-BB8E-3ED4E692C568}.Debug|Any CPU.ActiveCfg = Debug|Any CPU