cool
This commit is contained in:
19
Src/Unity/Scripts/SignalR/Net.BITKit.SignalR.Unity.asmdef
Normal file
19
Src/Unity/Scripts/SignalR/Net.BITKit.SignalR.Unity.asmdef
Normal 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
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 032072f82da34ae4895a3c477957e594
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
68
Src/Unity/Scripts/SignalR/SignalRClient.cs
Normal file
68
Src/Unity/Scripts/SignalR/SignalRClient.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
11
Src/Unity/Scripts/SignalR/SignalRClient.cs.meta
Normal file
11
Src/Unity/Scripts/SignalR/SignalRClient.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3430c184bfccd9468f9c938ed99ae48
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user