我无法通过 SignalR 从 Angular 向 Unity 发送消息
I can't send messages from Angular to Unity via SignalR
所以我一直在尝试在这两者之间建立任何形式的联系,我不知道是否遗漏了什么,但我无法在两者之间进行交流。
我希望能够在有人使用 Angular 进入房间时进行“通知”,并且我希望在 Unity 中“问候”进入房间的用户。
我似乎无法完成这项工作,我没有错误。
如果我在 Unity 中调用一个方法它可以很好地接收它,但是如果 Angular 调用方法 Unity 没有响应。
这里是所有相关的代码(据我所知)。
如果你弄明白了,这对我有很大帮助。
signalr.service.ts (angular)
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 (angular)
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");
});
Connector.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(统一)
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);
}
}
当它在 Unity 中 运行 这是控制台中的输出:
这是浏览器的输出:
如果我在 Angular 中关闭 WebSocket 传输选项,则 ID 的值为:
所以问题如下。
我的集线器正在客户端上调用方法“UpdateMessage”。
但是我的 Unity 连接监听的是错误的“SendMessage”。
在我这边看起来很简单。
我向任何试图弄明白的人道歉。
所以我一直在尝试在这两者之间建立任何形式的联系,我不知道是否遗漏了什么,但我无法在两者之间进行交流。
我希望能够在有人使用 Angular 进入房间时进行“通知”,并且我希望在 Unity 中“问候”进入房间的用户。
我似乎无法完成这项工作,我没有错误。
如果我在 Unity 中调用一个方法它可以很好地接收它,但是如果 Angular 调用方法 Unity 没有响应。
这里是所有相关的代码(据我所知)。
如果你弄明白了,这对我有很大帮助。
signalr.service.ts (angular)
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 (angular)
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");
});
Connector.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(统一)
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);
}
}
当它在 Unity 中 运行 这是控制台中的输出:
这是浏览器的输出:
如果我在 Angular 中关闭 WebSocket 传输选项,则 ID 的值为:
所以问题如下。
我的集线器正在客户端上调用方法“UpdateMessage”。
但是我的 Unity 连接监听的是错误的“SendMessage”。
在我这边看起来很简单。
我向任何试图弄明白的人道歉。