This commit is contained in:
CortexCore
2025-07-11 11:45:45 +08:00
parent fc189b98cc
commit ecae0f809c
76 changed files with 237471 additions and 33136 deletions

View File

@@ -0,0 +1,19 @@
{
"name": "Net.BITKit.SignalR.Unity",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"Disabled"
],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 032072f82da34ae4895a3c477957e594
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,68 @@
using System;
using Microsoft.AspNetCore.SignalR.Client;
using UnityEngine;
using System.Threading.Tasks;
using BITKit;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.Logging;
public class SignalRClient : MonoBehaviour
{
[SerializeField] private string url = @"https://yourserver.com/chathub";
private HubConnection _connection;
private ILogger<SignalRClient> _logger;
private StopWatcher _stopWatcher;
// 用于与 SignalR 服务器进行通信
private async void Start()
{
await UniTask.Delay(300);
if(destroyCancellationToken.IsCancellationRequested)return;
_connection = new HubConnectionBuilder()
.WithUrl(url)
.Build();
BITApp.ServiceProvider.QueryComponents(out _logger);
_connection.On<string, string>("ReceiveMessage", (user, message) =>
{
_stopWatcher?.Dispose();
_logger.LogInformation($"Received from Blazor/Server: {user} - {message}");
});
destroyCancellationToken.Register(Dispose);
await _connection.StartAsync();
_logger.LogInformation("Unity connected to SignalR server!");
// 发送消息给 Blazor
await SendMessageToBlazor("Unity", "Hello from Unity!");
for (var i = 0; i < 16; i++)
{
_stopWatcher = new StopWatcher(_logger, $"SignalR延迟 at {i}");
await SendMessageToBlazor("Unity", "OK,Let's Count!");
}
var id = await _connection.InvokeAsync<string>("GetMessage");
_logger.LogInformation($"获取用户ID:{id}");
}
private async void Dispose()
{
await _connection.StopAsync();
}
private async Task SendMessageToBlazor(string user, string message)
{
await _connection.InvokeAsync("SendMessage", user, message);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3430c184bfccd9468f9c938ed99ae48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: