Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding stream timeouts #492

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Example/Example.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -9,7 +9,13 @@
<OutputType>Exe</OutputType>
<RootNamespace>Example</RootNamespace>
<AssemblyName>example</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,6 +26,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
Expand All @@ -28,6 +35,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug_Ubuntu|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -38,6 +46,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release_Ubuntu|AnyCPU' ">
<DebugType>none</DebugType>
Expand All @@ -47,6 +56,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -68,4 +78,7 @@
<Compile Include="Notifier.cs" />
<Compile Include="NotificationMessage.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
</Project>
16 changes: 7 additions & 9 deletions Example/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ namespace Example
internal class Notifier : IDisposable
{
private volatile bool _enabled;
private ManualResetEvent _exited;
private Queue<NotificationMessage> _queue;
private object _sync;
private ManualResetEvent _waitHandle;

public Notifier ()
{
_enabled = true;
_exited = new ManualResetEvent (false);
_queue = new Queue<NotificationMessage> ();
_sync = ((ICollection) _queue).SyncRoot;
_waitHandle = new ManualResetEvent (false);

ThreadPool.QueueUserWorkItem (
state => {
Expand All @@ -40,9 +40,8 @@ public Notifier ()
}
}

_exited.Set ();
}
);
_waitHandle.Set ();
});
}

public int Count {
Expand All @@ -61,16 +60,15 @@ private NotificationMessage dequeue ()
public void Close ()
{
_enabled = false;
_exited.WaitOne ();
_exited.Close ();
_waitHandle.WaitOne ();
_waitHandle.Close ();
}

public void Notify (NotificationMessage message)
{
lock (_sync) {
lock (_sync)
if (_enabled)
_queue.Enqueue (message);
}
}

void IDisposable.Dispose ()
Expand Down
63 changes: 28 additions & 35 deletions Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,67 @@ public static void Main (string[] args)
// close status 1001 (going away) when the control leaves the using block.
//
// If you would like to connect to the server with the secure connection,
// you should create a new instance with a wss scheme WebSocket URL.
// you should create the instance with the wss scheme WebSocket URL.

using (var nf = new Notifier ())
using (var ws = new WebSocket ("ws://echo.websocket.org"))
using (var ws = new ClientWebSocket ("ws://echo.websocket.org"))
//using (var ws = new WebSocket ("wss://echo.websocket.org"))
//using (var ws = new WebSocket ("ws://localhost:4649/Echo"))
//using (var ws = new WebSocket ("wss://localhost:5963/Echo"))
//using (var ws = new WebSocket ("ws://localhost:4649/Echo?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:5963/Echo?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:4649/Echo"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat"))
//using (var ws = new WebSocket ("wss://localhost:5963/Chat"))
//using (var ws = new WebSocket ("ws://localhost:4649/Chat?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:5963/Chat?name=nobita"))
//using (var ws = new WebSocket ("wss://localhost:4649/Chat"))
{
// Set the WebSocket events.

ws.OnOpen += (sender, e) => ws.Send ("Hi, there!");

ws.OnMessage += (sender, e) =>
nf.Notify (
new NotificationMessage {
Summary = "WebSocket Message",
Body = !e.IsPing ? e.Data : "Received a ping.",
Icon = "notification-message-im"
}
);
nf.Notify (
new NotificationMessage {
Summary = "WebSocket Message",
Body = !e.IsPing ? e.Data : "Received a ping.",
Icon = "notification-message-im"
});

ws.OnError += (sender, e) =>
nf.Notify (
new NotificationMessage {
Summary = "WebSocket Error",
Body = e.Message,
Icon = "notification-message-im"
}
);
nf.Notify (
new NotificationMessage {
Summary = "WebSocket Error",
Body = e.Message,
Icon = "notification-message-im"
});

ws.OnClose += (sender, e) =>
nf.Notify (
new NotificationMessage {
Summary = String.Format ("WebSocket Close ({0})", e.Code),
Body = e.Reason,
Icon = "notification-message-im"
}
);
nf.Notify (
new NotificationMessage {
Summary = String.Format ("WebSocket Close ({0})", e.Code),
Body = e.Reason,
Icon = "notification-message-im"
});

#if DEBUG
// To change the logging level.
ws.Log.Level = LogLevel.Trace;

// To change the wait time for the response to the Ping or Close.
//ws.WaitTime = TimeSpan.FromSeconds (10);
ws.WaitTime = TimeSpan.FromSeconds (10);

// To emit a WebSocket.OnMessage event when receives a ping.
//ws.EmitOnPing = true;
ws.EmitOnPing = true;
#endif
// To enable the Per-message Compression extension.
//ws.Compression = CompressionMethod.Deflate;

// To validate the server certificate.
/*
/* To validate the server certificate.
ws.SslConfiguration.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => {
ws.Log.Debug (
String.Format (
"Certificate:\n- Issuer: {0}\n- Subject: {1}",
certificate.Issuer,
certificate.Subject
)
);
certificate.Subject));

return true; // If the server certificate is valid.
};
Expand All @@ -95,7 +88,7 @@ public static void Main (string[] args)
// To send the Origin header.
//ws.Origin = "http://localhost:4649";

// To send the cookies.
// To send the Cookies.
//ws.SetCookie (new Cookie ("name", "nobita"));
//ws.SetCookie (new Cookie ("roles", "\"idiot, gunfighter\""));

Expand Down
Loading