Skip to content

Commit

Permalink
completed v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
galvesribeiro committed Jul 15, 2018
1 parent 49602d4 commit 9decde9
Show file tree
Hide file tree
Showing 26 changed files with 337 additions and 262 deletions.
27 changes: 27 additions & 0 deletions .vsts-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
queue: 'Hosted VS2017'

variables:
buildConfiguration: 'Release'

steps:

- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
inputs:
command: pack
packagesToPack: 'src/Blazor.Extensions.SignalR/*.csproj'
configuration: '$(buildConfiguration)'
versioningScheme: byPrereleaseNumber
majorVersion: '0'
minorVersion: '1'
patchVersion: '0'
35 changes: 35 additions & 0 deletions .vsts-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
queue: 'Hosted VS2017'

variables:
buildConfiguration: 'Release'

steps:

- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
inputs:
command: pack
packagesToPack: 'src/Blazor.Extensions.SignalR/*.csproj'
packDirectory: '$(build.artifactStagingDirectory)'
configuration: '$(buildConfiguration)'
versioningScheme: byEnvVar
versionEnvVar: Version

- task: NuGetCommand@2
inputs:
command: push
packagesToPush: '$(build.artifactStagingDirectory)/*.nupkg'
publishFeedCredentials: 'OSS'
nuGetFeedType: external
versioningScheme: byEnvVar
versionEnvVar: Version
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
# SignalR
SignalR Core support for Microsoft AspNet Core Blazor
<!---[![Build status](https://img.shields.io/circleci/project/github/BlazorExtensions/SignalR.svg)](https://ci.dot.net/job/dotnet_orleans/job/master/)-->
[![Package Version](https://img.shields.io/nuget/v/Blazor.Extensions.SignalR.svg)](https://www.nuget.org/packages/Blazor.Extensions.SignalR)
[![NuGet Downloads](https://img.shields.io/nuget/dt/Blazor.Extensions.SignalR.svg)](https://www.nuget.org/packages/Blazor.Extensions.SignalR)
[![License](https://img.shields.io/github/license/BlazorExtensions/SignalR.svg)](https://github.com/BlazorExtensions/SignalR/blob/master/LICENSE)

# Blazor Extensions

Blazor Extensions is a set of packages with the goal of adding useful features to [Blazor](https://blazor.net).

# Blazor Extensions SignalR

This package adds a [Microsoft ASP.NET Core SignalR](https://github.com/aspnet/SignalR) client library for [Microsoft ASP.NET Blazor](https://github.com/aspnet/Blazor).

The package aims to mimic the C# APIs of SignalR Client as much as possible and it is developed by wrapping the TypeScript client by using Blazor's interop capabilities.

For more information about SignalR development, please check [SignalR documentation](https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-2.1).

# Features

This package implements all public features of SignalR Typescript client.

> Note: The _Streaming_ APIs are not implemented yet. We will add it soon.
# Sample usage

The following snippet shows how to setup the client to send and receive messages using SignalR.

```c#
var connection = new HubConnectionBuilder()
.WithUrl("/myHub", // The hub URL. If the Hub is hosted on the server where the blazor is hosted, you can just use the relative path.
opt =>
{
opt.LogLevel = SignalRLogLevel.Trace; // Client log level
opt.Transport = HttpTransportType.WebSockets; // Which transport you want to use for this connection
})
.Build(); // Build the HubConnection
connection.On("Receive", this.Handle); // Subscribe to messages sent from the Hub to the "Receive" method by passing a handle (Func<object, Task>) to process messages.
await connection.StartAsync(); // Start the connection.
await connection.InvokeAsync("ServerMethod", param1, param2, paramX); // Invoke a method on the server called "ServerMethod" and pass parameters to it.
var result = await connection.InvokeAsync<MyResult>("ServerMethod", param1, param2, paramX); // Invoke a method on the server called "ServerMethod", pass parameters to it and get the result back.
```

# Contributions and feedback

Please feel free to use the component, open issues, fix bugs or provide feedback.

# Contributors

The following people are the maintainers of the Blazor Extensions projects:

- [Attila Hajdrik](https://github.com/attilah)
- [Gutemberg Ribiero](https://github.com/galvesribeiro)
15 changes: 0 additions & 15 deletions SignalR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Extensions.SignalR.JS", "src\Blazor.Extensions.SignalR.JS\Blazor.Extensions.SignalR.JS.csproj", "{1C49147F-7C73-4962-A71C-6A193970D058}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Extensions.SignalR.Test.Shared", "test\Blazor.Extensions.SignalR.Test.Shared\Blazor.Extensions.SignalR.Test.Shared.csproj", "{8848C68C-2E31-475F-906E-F5E500ADCEFD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Extensions.SignalR.Test.Client", "test\Blazor.Extensions.SignalR.Test.Client\Blazor.Extensions.SignalR.Test.Client.csproj", "{752EC32C-EC70-4E7E-B2C3-610B0893249F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Extensions.SignalR.Test.Server", "test\Blazor.Extensions.SignalR.Test.Server\Blazor.Extensions.SignalR.Test.Server.csproj", "{8952910D-6251-48A0-8A00-369553370547}"
Expand Down Expand Up @@ -56,18 +54,6 @@ Global
{1C49147F-7C73-4962-A71C-6A193970D058}.Release|x64.Build.0 = Release|Any CPU
{1C49147F-7C73-4962-A71C-6A193970D058}.Release|x86.ActiveCfg = Release|Any CPU
{1C49147F-7C73-4962-A71C-6A193970D058}.Release|x86.Build.0 = Release|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Debug|x64.ActiveCfg = Debug|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Debug|x64.Build.0 = Debug|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Debug|x86.ActiveCfg = Debug|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Debug|x86.Build.0 = Debug|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Release|Any CPU.Build.0 = Release|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Release|x64.ActiveCfg = Release|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Release|x64.Build.0 = Release|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Release|x86.ActiveCfg = Release|Any CPU
{8848C68C-2E31-475F-906E-F5E500ADCEFD}.Release|x86.Build.0 = Release|Any CPU
{752EC32C-EC70-4E7E-B2C3-610B0893249F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{752EC32C-EC70-4E7E-B2C3-610B0893249F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{752EC32C-EC70-4E7E-B2C3-610B0893249F}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -99,7 +85,6 @@ Global
GlobalSection(NestedProjects) = preSolution
{9378C7BF-0899-4835-B8FB-099292C8C63D} = {B286BCBD-DAD8-4DE7-9334-3DE18DF233AF}
{1C49147F-7C73-4962-A71C-6A193970D058} = {B286BCBD-DAD8-4DE7-9334-3DE18DF233AF}
{8848C68C-2E31-475F-906E-F5E500ADCEFD} = {20DAA632-F8AD-4C5F-9E5F-FC82B7CB56A7}
{752EC32C-EC70-4E7E-B2C3-610B0893249F} = {20DAA632-F8AD-4C5F-9E5F-FC82B7CB56A7}
{8952910D-6251-48A0-8A00-369553370547} = {20DAA632-F8AD-4C5F-9E5F-FC82B7CB56A7}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<ItemGroup>
<WebpackInputs Remove="src\BlazorTypes.ts" />
<WebpackInputs Remove="src\GlobalExports.ts" />
<WebpackInputs Remove="src\MessageTypes.ts" />
</ItemGroup>

<Target Name="EnsureNpmRestored" Condition="!Exists('node_modules')">
Expand Down
13 changes: 9 additions & 4 deletions src/Blazor.Extensions.SignalR.JS/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Blazor.Extensions.SignalR.JS/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ts-loader": "^4.4.2"
},
"dependencies": {
"@aspnet/signalr": "^1.0.2"
"@aspnet/signalr": "^1.0.2",
"@aspnet/signalr-protocol-msgpack": "^1.0.2"
}
}
85 changes: 63 additions & 22 deletions src/Blazor.Extensions.SignalR.JS/src/HubConnectionManager.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,64 @@
import * as signalR from "@aspnet/signalr";
import * as sianglRMessagePack from "@aspnet/signalr-protocol-msgpack";
import { BlazorType, MethodIdentifier, TypeIdentifier } from './BlazorTypes';
import { ConnectionOperation, MessagePacket } from './MessageTypes';

export class HubConnectionManager {
private _hubConnections: Map<string, signalR.HubConnection> = new Map<string, signalR.HubConnection>();

public createConnection(connectionId: string, url: string, transportOptions: signalR.IHttpConnectionOptions) {
public createConnection(connectionId: string, url: string, transportOptions: signalR.IHttpConnectionOptions, addMessagePack: boolean) {
if (!connectionId) throw new Error('Invalid connectionId.');
if (!url) throw new Error('Invalid hub url.');
if (!transportOptions) throw new Error('Invalid transport options.');

if (this._hubConnections[connectionId]) return;

const connection = new signalR.HubConnectionBuilder()
.withUrl(url, transportOptions)
.build();
if (addMessagePack) {
const connection = new signalR.HubConnectionBuilder()
.withUrl(url, transportOptions)
.withHubProtocol(new sianglRMessagePack.MessagePackHubProtocol())
.build();

this._hubConnections.set(connectionId, connection);
this._hubConnections.set(connectionId, connection);
} else {
const connection = new signalR.HubConnectionBuilder()
.withUrl(url, transportOptions)
.build();

this._hubConnections.set(connectionId, connection);
}
}

public removeConnection(connectionId: string) {
this._hubConnections.delete(connectionId);
}

public startConnection = (connectionOperation: ConnectionOperation): Promise<void> => {
console.log(connectionOperation);
const connection = this.getConnection(connectionOperation.connectionId);
public startConnection = (connectionId: string): Promise<void> => {
const connection = this.getConnection(connectionId);

return connection.start();
}

public stopConnection = (connectionOperation: ConnectionOperation): Promise<void> => {
const connection = this.getConnection(connectionOperation.connectionId);
public stopConnection = (connectionId: string): Promise<void> => {
const connection = this.getConnection(connectionId);

return connection.stop();
}

public invokeAsync = (connectionId: string, methodName: string, ...args: any[]): Promise<void> => {
const connection = this.getConnection(connectionId);

return connection.invoke(methodName, ...args);
}

public invokeWithResultAsync = (connectionId: string, methodName: string, ...args: any[]): Promise<any> => {
const connection = this.getConnection(connectionId);

return connection.invoke(methodName, ...args);
}

private getConnection = (connectionId: string) => {
//console.log(connectionId);
//console.log(this._hubConnections);
if (!connectionId) throw new Error('Invalid connectionId.');
const connection = this._hubConnections.get(connectionId);
//console.log(connection);
if (!connection) throw new Error('Invalid connection.');

return connection;
Expand All @@ -59,40 +76,64 @@ export class HubConnectionManager {
{
type: {
assembly: 'Blazor.Extensions.SignalR',
name: 'HubConnectionManager'
name: 'Blazor.Extensions.HubConnectionManager'
},
method: {
name: 'Dispatch'
}
}, { connectionId: connectionId, methodName: methodName, payload: payload });
}, connectionId, methodName, payload);
}

public static initialize() {
const Blazor: BlazorType = window["Blazor"];
window["BlazorExtensions"].HubConnectionManager = new HubConnectionManager();

Blazor.registerFunction('Blazor.Extensions.SignalR.CreateConnection', (connectionId: string, url: string, httpConnectionOptions: any) => {
Blazor.registerFunction('Blazor.Extensions.SignalR.CreateConnection', (connectionId: string, url: string, httpConnectionOptions: any, addMessagePack: boolean) => {
window["BlazorExtensions"].HubConnectionManager.createConnection(connectionId, url,
{
logger: httpConnectionOptions.LogLevel,
transport: httpConnectionOptions.Transport,
logMessageContent: httpConnectionOptions.LogMessageContent,
skipNegotiation: httpConnectionOptions.SkipNegotiation,
accessTokenFactory: () => httpConnectionOptions.AccessToken
});
}, addMessagePack);
return true;
});

Blazor.registerFunction('Blazor.Extensions.SignalR.RemoveConnection', (connectionId: string) => {
return window["BlazorExtensions"].HubConnectionManager.removeConnection(connectionId);
});

Blazor.registerFunction('Blazor.Extensions.SignalR.StartConnection', (connectionOperation: ConnectionOperation) => {
return window["BlazorExtensions"].HubConnectionManager.startConnection(connectionOperation);
Blazor.registerFunction('Blazor.Extensions.SignalR.StartConnection', (connectionId: string) => {
//TODO remove this parse after Blazor fixed the async args json parsing code
const parsedConnectionId = JSON.parse(connectionId);

return window["BlazorExtensions"].HubConnectionManager.startConnection(parsedConnectionId);
});

Blazor.registerFunction('Blazor.Extensions.SignalR.StopConnection', (connectionId: string) => {
//TODO remove this parse after Blazor fixed the async args json parsing code
const parsedConnectionId = JSON.parse(connectionId);

return window["BlazorExtensions"].HubConnectionManager.stopConnection(parsedConnectionId);
});

Blazor.registerFunction('Blazor.Extensions.SignalR.StopConnection', (connectionOperation: ConnectionOperation) => {
return window["BlazorExtensions"].HubConnectionManager.stopConnection(connectionOperation);
Blazor.registerFunction('Blazor.Extensions.SignalR.InvokeAsync', (connectionId: string, methodName: string, args: any) => {
//TODO remove this parse after Blazor fixed the async args json parsing code
const parsedConnectionId = JSON.parse(connectionId);
const parsedMethodName = JSON.parse(methodName);
const parsedArgs = JSON.parse(args);

return window["BlazorExtensions"].HubConnectionManager.invokeAsync(parsedConnectionId, parsedMethodName, ...parsedArgs);
});

Blazor.registerFunction('Blazor.Extensions.SignalR.InvokeWithResultAsync', (connectionId: string, methodName: string, args: any) => {
//TODO remove this parse after Blazor fixed the async args json parsing code
const parsedConnectionId = JSON.parse(connectionId);
const parsedMethodName = JSON.parse(methodName);
const parsedArgs = JSON.parse(args);

return window["BlazorExtensions"].HubConnectionManager.invokeWithResultAsync(parsedConnectionId, parsedMethodName, ...parsedArgs);
});

Blazor.registerFunction('Blazor.Extensions.SignalR.On', (connectionId: string, methodName: string) => {
Expand Down
8 changes: 0 additions & 8 deletions src/Blazor.Extensions.SignalR.JS/src/MessageTypes.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/Blazor.Extensions.SignalR/HttpConnectionOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Blazor.Extensions
{
public class HttpConnectionOptions
Expand Down
Loading

0 comments on commit 9decde9

Please sign in to comment.