uSignalR
uSignalR copied to clipboard
Cannot get subscribe to return value
Hi,
I'm trying out this package, and it works. I'm using a .Net 4.5 backend, and connecting and sending messages works, but I cannot get the Unity GameManager to subscribe to my items. This is my Hub: `using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Backend.Models; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs;
namespace Backend.Hubs
{
[HubName("DefaultHub")]
public class DefaultHub : Hub
{
private readonly IHubGroups hubGroups = TinyIoC.TinyIoCContainer.Current.Resolve<HubGroups>();
public static List
[HubMethodName("StartRoom")]
public void StartRoom(string roomName)
{
Task.Run(() => CurrentRoomNames.Add(roomName));
var queryStr = Context.QueryString["UserName"];
var user = new HubClientModel(queryStr, Context.ConnectionId);
Groups.Add(Context.ConnectionId, roomName);
Task.Run(() => hubGroups.AddNewGroup(roomName, user));
Clients.All.newGroupAdded(roomName, Context.ConnectionId);
}
[HubMethodName("JoinRoom")]
public void JoinRoom(string roomName)
{
Clients.All.addChatMessageToPage("Test", "Bericht");
Task.Run(() =>
{
Groups.Add(Context.ConnectionId, roomName);
});
var queryStr = Context.QueryString["UserName"];
if (CurrentRoomNames.Contains(roomName) && hubGroups.AllHubGroupModels().FirstOrDefault(x => x.GroupName == roomName) != null)
{
var user = new HubClientModel(queryStr, Context.ConnectionId);
Clients.All.notifyOnNewUser(Context.ConnectionId);
Task.Run(() => hubGroups.AddUserToGroup(roomName, user));
Clients.All.newUserAddedToGroup(roomName, Context.ConnectionId);
}
else
{
Task.Run(() => CurrentRoomNames.Add(roomName));
var user = new HubClientModel(queryStr, Context.ConnectionId);
Clients.All.notifyOnNewUser(Context.ConnectionId);
Groups.Add(Context.ConnectionId, roomName);
Task.Run(() => hubGroups.AddNewGroup(roomName, user));
Clients.All.newGroupAdded(roomName, Context.ConnectionId);
Clients.All.newUserAddedToGroup(roomName, Context.ConnectionId);
}
}
[HubMethodName("LeaveRoom")]
public Task LeaveRoom(string roomName)
{
Task.Run(() => hubGroups.RemoveUserFromGroup(roomName, Context.ConnectionId));
Clients.All.userRemovedFromGroup(roomName, Context.ConnectionId);
return Groups.Remove(Context.ConnectionId, roomName);
}
[HubMethodName("NewChatMessage")]
public void NewChatMessage(string name, string message)
{
Clients.All.addChatMessageToPage(name, message);
}
}
}`
This is my GameManager: `using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using uSignalR;
public class GameManager : MonoBehaviour {
public static GameManager Instance { get; private set; }
public GameObject UsernameInputGameObject;
public GameObject MainMenu;
public GameObject GameObjectToReturnTo;
public AudioSource backgroundMusic;
public AudioSource soundEffects;
public AudioClip StartupSound;
public string Username;
public string SignalRUrl;
public string GameId;
public bool PlaySounds = true;
public int ConnectedPlayers = 0;
Connection connection;
uSignalR.Hubs.IHubProxy proxy;
// Notify other screens when actions from SignalR happen
public delegate void PlayerJoined();
public event PlayerJoined OnPlayerJoined;
public void GoToUsernameInputCanvas(GameObject gameObjectToReturnTo = null) {
if (gameObjectToReturnTo == null) {
GameObjectToReturnTo = null;
} else {
GameObjectToReturnTo = gameObjectToReturnTo;
gameObjectToReturnTo.SetActive (false);
}
UsernameInputGameObject.SetActive (true);
UsernameInputGameObject.GetComponent<Canvas> ().enabled = true;
}
void Awake()
{
connection = new uSignalR.Connection(SignalRUrl + "signalr");
#if UNITY_EDITOR
PlaySounds = false;
#endif
if (Instance == null) {
Instance = this;
} else {
Destroy (gameObject);
}
DontDestroyOnLoad (gameObject);
PlaySound (StartupSound);
GetSignalRConnection ();
}
public void SaveUsername(string username) {
Username = username;
}
void OnApplicationQuit()
{
LeaveGame ();
Debug.Log("Application ending after " + Time.time + " seconds");
}
public void GetSignalRConnection() {
// setup proxy
var succes = connection.Start().ContinueWith((result) => {
if (result.IsFaulted) {
Debug.Log("No connection could be made");
} else {
Debug.Log("Connected");
proxy = new uSignalR.Hubs.HubProxy(connection, "DefaultHub");
proxy.Subscribe("addChatMessageToPage").Data += data =>
{
Debug.Log(data[0] + ":" + data[1]);
};
proxy.Subscribe("sendMessage").Data += data =>
{
Debug.Log(data[0]);
};
proxy.Subscribe("notifyOnNewUser").Data += data =>
{
Debug.Log("Yes: " + data[0]);
if (OnPlayerJoined != null)
OnPlayerJoined();
};
proxy.Subscribe("NotifyOnNewUser").Data += data =>
{
Debug.Log("Yes: " + data[0]);
if (OnPlayerJoined != null)
OnPlayerJoined();
};
}
});
}
public void JoinGame(string id) {
proxy.Invoke("JoinRoom", id);
GameId = id;
}
public void StartNewGame(string id) {
proxy.Invoke("StartRoom", id);
GameId = id;
}
public void LeaveGame() {
proxy.Invoke ("LeaveRoom", GameManager.Instance.GameId);
//uSignalR.Hubs.Send ("Test");
GameId = "";
}
public void PlaySound(AudioClip clip) {
if (PlaySounds) {
soundEffects.clip = clip;
soundEffects.Play ();
}
}
public void ToggleSound() {
PlaySounds = !PlaySounds;
}
public void ExitGame() {
Application.Quit ();
}
} `
I set breakpoints on all subscribers, but nothing happens. Could you tell me why? I've used https://github.com/jenyayel/SignalR.Client.20 and it works, but not if there's any connection issue, so I thought I'd try this one.
I am using Unity 2017.1.0f Personal.
Hello Sibren, long time have passed since you posted this. I am running into the similar issue. Where you able to find a workaround? Any other path you tried after this? Please let me know, thank you! Alberto