gromchen / usignalr Goto Github PK
View Code? Open in Web Editor NEWSignalR which works in Unity3D
License: Apache License 2.0
SignalR which works in Unity3D
License: Apache License 2.0
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();
public static List CurrentRoomNames = new 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.
I can't compile the project, it has over 30 errors without the Newtonsoft.Json (178 with it), for example, it says "JsonProperty" etc. Attribute requires System.Runtime assembly.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.