Skip to content

Commit 38f42d3

Browse files
committed
"Hello World" SignalR sample with OWin self host
1 parent 2583735 commit 38f42d3

File tree

12 files changed

+496
-0
lines changed

12 files changed

+496
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignalrOwinHelloWorld", "SignalrOwinHelloWorld\SignalrOwinHelloWorld.csproj", "{52D26B64-4730-4713-AA88-876A61817477}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{52D26B64-4730-4713-AA88-876A61817477}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{52D26B64-4730-4713-AA88-876A61817477}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{52D26B64-4730-4713-AA88-876A61817477}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{52D26B64-4730-4713-AA88-876A61817477}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
15+
</dependentAssembly>
16+
<dependentAssembly>
17+
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
18+
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
19+
</dependentAssembly>
20+
<dependentAssembly>
21+
<assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
22+
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
23+
</dependentAssembly>
24+
</assemblyBinding>
25+
</runtime>
26+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SignalrOwinHelloWorld
2+
{
3+
public class Game
4+
{
5+
public int GameId { get; set; }
6+
public string Connection1 { get; set; }
7+
public string Connection2 { get; set; }
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Microsoft.AspNet.SignalR;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SignalrOwinHelloWorld
6+
{
7+
public class GameHub : Hub
8+
{
9+
// Internal list of games
10+
private static List<Game> games = new List<Game>();
11+
12+
/// <summary>
13+
/// Method called by a client if a user wants to start a game
14+
/// </summary>
15+
/// <param name="gameId">ID of the new game</param>
16+
public void StartGame(int gameId)
17+
{
18+
// Store the game's data in memory
19+
GameHub.games.Add(new Game
20+
{
21+
GameId = gameId,
22+
Connection1 = this.Context.ConnectionId
23+
});
24+
}
25+
26+
/// <summary>
27+
/// Method called by a client if a user wants to join a game
28+
/// </summary>
29+
/// <param name="gameId">ID of the game to join</param>
30+
/// <returns>
31+
/// SignalR connection ID of the partner or null if <paramref name="gameID"/> is unknown.
32+
/// </returns>
33+
public string JoinGame(int gameId)
34+
{
35+
// Find game with specified game ID
36+
var game = GameHub.games.SingleOrDefault(g => g.GameId == gameId);
37+
if (game == null)
38+
{
39+
// No such game found
40+
return null;
41+
}
42+
43+
// Make sure that game does not already have two players
44+
if (game.Connection2 == null)
45+
{
46+
// Add second player to game
47+
game.Connection2 = this.Context.ConnectionId;
48+
49+
// Inform first player that second player has arrived
50+
this.Clients.Client(game.Connection1).PlayerArrived(game.Connection2);
51+
52+
return game.Connection1;
53+
}
54+
else
55+
{
56+
// Game already has to players
57+
return null;
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Method called by a client when player fired
63+
/// </summary>
64+
public void Fire()
65+
{
66+
// Find game associated with the current connection ID
67+
var game = GameHub.games.SingleOrDefault(g => g.Connection1 == this.Context.ConnectionId || g.Connection2 == this.Context.ConnectionId);
68+
if (game != null)
69+
{
70+
// Inform other player that she got shot
71+
this.Clients.Client(game.Connection1 == this.Context.ConnectionId ? game.Connection2 : game.Connection1).GotShot();
72+
}
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.Owin.Hosting;
2+
using System;
3+
4+
namespace SignalrOwinHelloWorld
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Start a self-hosting web server
11+
const string baseUrl = "http://localhost:12345";
12+
using (WebApp.Start<Startup>(baseUrl))
13+
{
14+
Console.WriteLine($"Server is listening on {baseUrl}");
15+
Console.WriteLine("Press any key to quit");
16+
Console.ReadKey();
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SignalrOwinHelloWorld")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SignalrOwinHelloWorld")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("52d26b64-4730-4713-aa88-876a61817477")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{52D26B64-4730-4713-AA88-876A61817477}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SignalrOwinHelloWorld</RootNamespace>
11+
<AssemblyName>SignalrOwinHelloWorld</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Microsoft.AspNet.SignalR.Core, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Microsoft.AspNet.SignalR.Core.2.2.0\lib\net45\Microsoft.AspNet.SignalR.Core.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
42+
<Private>True</Private>
43+
</Reference>
44+
<Reference Include="Microsoft.Owin.Cors, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Microsoft.Owin.Cors.3.0.1\lib\net45\Microsoft.Owin.Cors.dll</HintPath>
46+
<Private>True</Private>
47+
</Reference>
48+
<Reference Include="Microsoft.Owin.Diagnostics, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
49+
<HintPath>..\packages\Microsoft.Owin.Diagnostics.3.0.1\lib\net45\Microsoft.Owin.Diagnostics.dll</HintPath>
50+
<Private>True</Private>
51+
</Reference>
52+
<Reference Include="Microsoft.Owin.FileSystems, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<HintPath>..\packages\Microsoft.Owin.FileSystems.3.0.1\lib\net45\Microsoft.Owin.FileSystems.dll</HintPath>
54+
<Private>True</Private>
55+
</Reference>
56+
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
57+
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.3.0.1\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
58+
<Private>True</Private>
59+
</Reference>
60+
<Reference Include="Microsoft.Owin.Hosting, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61+
<HintPath>..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
62+
<Private>True</Private>
63+
</Reference>
64+
<Reference Include="Microsoft.Owin.Security, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
65+
<HintPath>..\packages\Microsoft.Owin.Security.3.0.1\lib\net45\Microsoft.Owin.Security.dll</HintPath>
66+
<Private>True</Private>
67+
</Reference>
68+
<Reference Include="Microsoft.Owin.StaticFiles, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
69+
<HintPath>..\packages\Microsoft.Owin.StaticFiles.3.0.1\lib\net45\Microsoft.Owin.StaticFiles.dll</HintPath>
70+
<Private>True</Private>
71+
</Reference>
72+
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
73+
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
74+
<Private>True</Private>
75+
</Reference>
76+
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
77+
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
78+
<Private>True</Private>
79+
</Reference>
80+
<Reference Include="System" />
81+
<Reference Include="System.Core" />
82+
<Reference Include="System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
83+
<HintPath>..\packages\Microsoft.AspNet.Cors.5.2.3\lib\net45\System.Web.Cors.dll</HintPath>
84+
<Private>True</Private>
85+
</Reference>
86+
<Reference Include="System.Xml.Linq" />
87+
<Reference Include="System.Data.DataSetExtensions" />
88+
<Reference Include="Microsoft.CSharp" />
89+
<Reference Include="System.Data" />
90+
<Reference Include="System.Net.Http" />
91+
<Reference Include="System.Xml" />
92+
</ItemGroup>
93+
<ItemGroup>
94+
<Compile Include="Game.cs" />
95+
<Compile Include="GameHub.cs" />
96+
<Compile Include="Program.cs" />
97+
<Compile Include="Properties\AssemblyInfo.cs" />
98+
<Compile Include="Startup.cs" />
99+
</ItemGroup>
100+
<ItemGroup>
101+
<None Include="App.config" />
102+
<None Include="packages.config" />
103+
<None Include="readme.md" />
104+
</ItemGroup>
105+
<ItemGroup>
106+
<Content Include="wwwroot\index.html">
107+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
108+
</Content>
109+
<Content Include="wwwroot\index.js">
110+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
111+
</Content>
112+
</ItemGroup>
113+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
114+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
115+
Other similar extension points exist, see Microsoft.Common.targets.
116+
<Target Name="BeforeBuild">
117+
</Target>
118+
<Target Name="AfterBuild">
119+
</Target>
120+
-->
121+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Owin.Cors;
2+
using Microsoft.Owin.FileSystems;
3+
using Microsoft.Owin.StaticFiles;
4+
using Owin;
5+
using System.IO;
6+
using System.Reflection;
7+
8+
namespace SignalrOwinHelloWorld
9+
{
10+
public class Startup
11+
{
12+
public void Configuration(IAppBuilder app)
13+
{
14+
// Setup static file serving
15+
app.UseFileServer(new FileServerOptions
16+
{
17+
FileSystem = new PhysicalFileSystem(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "wwwroot")),
18+
EnableDefaultFiles = true
19+
});
20+
21+
// Enable access from every URL with CORS
22+
app.UseCors(CorsOptions.AllowAll);
23+
24+
// Map SignalR hubs
25+
app.MapSignalR();
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net461" />
4+
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net461" />
5+
<package id="Microsoft.AspNet.SignalR.SelfHost" version="2.2.0" targetFramework="net461" />
6+
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net461" />
7+
<package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net461" />
8+
<package id="Microsoft.Owin.Diagnostics" version="3.0.1" targetFramework="net461" />
9+
<package id="Microsoft.Owin.FileSystems" version="3.0.1" targetFramework="net461" />
10+
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net461" />
11+
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net461" />
12+
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net461" />
13+
<package id="Microsoft.Owin.SelfHost" version="3.0.1" targetFramework="net461" />
14+
<package id="Microsoft.Owin.StaticFiles" version="3.0.1" targetFramework="net461" />
15+
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
16+
<package id="Owin" version="1.0" targetFramework="net461" />
17+
</packages>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SignalR "Hello World" Sample
2+
3+
## Introduction
4+
5+
This quite small sample can be used to describe the general idea
6+
of [SignalR](http://www.asp.net/signalr). It sets up a self-hosted
7+
OWin server (command line exe) with a server-side SignalR hub
8+
and a simple JavaScript client.
9+
10+
With the client, users can start a game or join an existing game.
11+
Once a user starts a game, it waits for another user to join.
12+
Once a second user joined, both players can start shooting. Each
13+
game has exactly two players.
14+
15+
## NuGet Packages
16+
17+
This samples uses the following NuGet packages:
18+
19+
- `Microsoft.AspNet.SignalR.SelfHost`
20+
- `Microsoft.Owin.Cors`
21+
- `Microsoft.Owin.StaticFiles`
22+
23+
## Components of the Sample
24+
25+
- [Startup.cs](Startup.cs) contains the OWin startup code
26+
- [GameHub.cs](GameHub.cs) contains the server-side SignalR hub
27+
- [index.html](wwwroot/index.html) and [index.js](wwwroot/index.js)
28+
contain the client-side SignalR code

0 commit comments

Comments
 (0)