所以我一直試圖在這兩者之間建立任何形式的聯系,我不知道是否遺漏了什么,但我無法在兩者之間進行交流。
當有人使用 Angular 進入房間時,我希望能夠“宣布”,并且我想在 Unity 中“問候”進入房間的用戶。
我似乎無法完成這項作業,我沒有錯誤。
如果我在 Unity 中呼叫一個方法,它可以很好地接收它,但如果 Angular 呼叫該方法,Unity 沒有回應。
所以這里是所有相關的代碼(我知道的)。
如果您弄清楚了,這將對我有很大幫助。
signalr.service.ts(角度)
hubConnection: signalR.HubConnection;
async StartConnection(): Promise<signalR.HubConnection>
{
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(environment.hubURL "Blackjack", {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
})
.build();
try
{
await this.hubConnection.start();
console.log("Hub connection started!");
return new Promise<signalR.HubConnection>((resolve) => { resolve(this.hubConnection) });
} catch (err)
{
return new Promise<signalR.HubConnection>((resolve, reject) =>
{ reject(console.error("Error while starting connection" err)) });
}
}
blackjack.component.ts(角度)
ngOnInit(): void
{
this.signalrService.StartConnection().then((hubConnection) => {
this.signalrService.GetUser().subscribe(user => this.JoinRoom(user));
console.log("ID: " hubConnection.connectionId);
});
this.signalrService.hubConnection.on("JoinRoomResponse", (user) => this.OnJoinRoom(user));
}
BlackjackHub(服務器)
public class BlackjackHub : Hub
{
public async Task JoinRoom(User user)
{
await Clients.All.SendAsync("JoinRoomResponse", user);
}
public async Task SendMessage(string author, string message)
{
await Clients.All.SendAsync("UpdateMessage", author, message);
}
}
程式和啟動(服務器初始化)
app.UseWebSockets();
app.UseEndpoints(x => {
x.MapControllers();
x.MapHub<BlackjackHub>("Blackjack");
});
連接器.cs(統一)
public class Connector
{
public Action<User> OnConnected;
public Action<string, string> OnMessageRecieved;
public Action OnDeal;
private HubConnection _connection;
public async Task InitAsync()
{
_connection = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/Blackjack", HttpTransportType.WebSockets)
.Build();
_connection.On<User>("JoinRoomResponse", (user) =>
{
OnConnected?.Invoke(new User(user));
});
_connection.On("DealCards", () => OnDeal?.Invoke());
_connection.On<string, string>("SendMessage", (author, message) =>
{
OnMessageRecieved?.Invoke(author, message);
});
await StartConnectionAsync();
}
private async Task StartConnectionAsync()
{
try
{
await _connection.StartAsync();
Debug.Log($"Hub Connection State : {_connection.State} \nConnection ID : {_connection.ConnectionId}");
}
catch (Exception ex)
{
Debug.LogError(ex);
}
}
}
NetworkManager.cs (unity)
public class NetworkManager : MonoBehaviour
{
private static NetworkManager _instance;
public static NetworkManager Instance
{
get { return _instance; }
}
private Connector _connector;
public List<User> users = new List<User>();
private void Awake()
{
if(_instance != null && _instance != this)
{
Destroy(this);
}
else
{
_instance = this;
}
}
private void Start()
{
StartCoroutine(nameof(StartAsync));
}
public async Task StartAsync()
{
_connector = new Connector();
_connector.OnConnected = OnConnected;
_connector.OnMessageRecieved = OnMessageRecieved;
await _connector.InitAsync();
}
public async void OnMessageRecieved(string author, string message)
{
Debug.Log($"{author}: {message}");
await Task.Delay(1);
}
public async void OnConnected (User user)
{
Debug.Log($"{user.Email} just joined.");
users.Add(user);
await Task.Delay(1);
}
}
When its run in Unity this is the output in the console:

Here is the output from the browser:

If I turn off the WebSocket Transport option in Angular the ID has a value:

uj5u.com熱心網友回復:
所以問題如下。
我的集線器在客戶端上呼叫方法“UpdateMessage”。
但是我的 Unity 連接正在監聽“SendMessage”,這是錯誤的。
在我這邊非常簡單。
我向任何試圖弄清楚這一點的人道歉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/438292.html
標籤:asp.net angular typescript unity3d signalr
上一篇:雪碧在本地檔案夾中沒有顯示?
