Skip to content

xAdamQ/Unity-WebGL-SignalR

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity WebGL SignalR

Repo Components

  • Unity: Unity3D project with Core SignalR library plugin (for editor / WebGL) and example scene
  • Server: ASP.NET Core project with a single SignalR hub for connection (CORS enabled)
  • Client: Node.js/Express project to serve built WebGL files from Unity

The hub is set to receive and broadcast a global message that invokes the listeners of all connected clients.

This functionality could be greatly extended for actual use in games or applications.

Library

The files needed for importing as a 'Plugin' in a Unity3D project are located at:

Unity/Assets/Plugins/SignalR

Client C# Packages

  • Microsoft.AspNetCore.SignalR.Client - 3.1.2

This and all dependencies were manually imported from NuGet using builds targeting .NET Standard 2.0. For specific versions, see:

Unity/Assets/Plugins/SignalR/Packages/Versions.txt

Note: These packages are only needed for use in the editor.

Client JS File

Once the WebGL project is built, the following must be referenced in the 'head' section of index.html:

https://cdn.jsdelivr.net/npm/@microsoft/[email protected]/dist/browser/signalr.min.js

Source

Usage

Attach a script file to a GameObject in your scene and add the following code to your 'Start' method:

srLib = new SignalRLib();
srLib.Init("<SignalRHubURL>", "<HubListenerName>");

srLib.ConnectionStarted += (object sender, MessageEventArgs e) =>
{
    Debug.Log(e.message);
    srLib.SendMessage("<HubMethodName>", "<MessageToSend>");
};

srLib.MessageReceived += (object sender, MessageEventArgs e) =>
{
    Debug.Log(e.message);
};

Properties

Properties

Hub

  • SignalR Hub URL: Endpoint for the target SignalR hub
  • Hub Listener Name: Name of listener to be invoked by the hub
  • Hub Method Name: Name of method in the Hub to receive message
  • Message to Send: Message to send to the hub

Successful Result

Result

References

About

Library with example components using Core SignalR in Unity WebGL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 57.6%
  • CSS 16.4%
  • JavaScript 15.5%
  • HTML 10.5%