I develop a webforms asp.net site with signalr to real time communications.
I have master.page with everything related to signalr defined.
<%: Scripts.Render("~/Scripts/jquery-1.11.0.min.js") %>
<%: Scripts.Render("~/Scripts/jquery.signalR-2.0.3.min.js") %>
<%: Scripts.Render("~/signalr/hubs") %>
In one content page i added another js file to handle signalr stuff, like this:
$(function () {
$.connection.hub.start()
.done(function () {
console.log('Now connected, connection ID=' + connection.hub.id);
});
});
Everything fine!
Then i tried to spread the access to signalR hub to others pages including the master page.
So, i put this in master direct on page:
<script type="text/javascript">
$(function () {
$.connection.hub.start()
.done(function () {
console.log('Now connected MASTER, connection ID=' + $.connection.hub.id);
});
});
</script>
All fine, but the client connects two time, but with same connectionID. Is there anyhow i can connect just one time and share HubProxy between master and content pages??
But, when i try to render another content page based on that master, i've got a error "hub is undefined".
The only page works ok with that master page is the first one with js separated to signalr.
here is the hub code as required:
public class GameHub : Hub
{
private const long tempoJogada = 30000;
private const long tempoTruco = 30000;
private const long tempoMao11 = 30000;
private readonly static Model.Truco _truco = new Model.Truco();
private Mesa _mesa;
private static int JogadoresOnline = 0;
public GameHub()
{
_mesa = _truco.Saloes[0].Mesas[0];
Debug.WriteLine("criou hub : " + DateTime.Now);
}
public List<Jogador> RetornaJogadoresMesa()
{
return _mesa.Jogadores;
}
public async Task<int> EntrarMesa(Jogador jogador)
{
int posicaoMesa = 0;
Jogador jogadorServer = _mesa.Jogadores.SingleOrDefault(jo => jo.Nome == jogador.Nome);
jogador.ClientGuid = new Guid(Context.ConnectionId);
if (_mesa.Jogadores.Count(jo => jo.Nome == jogador.Nome) == 0)
{
posicaoMesa = _mesa.EntrarMesa(jogador);
if (posicaoMesa > 0)
{
await Groups.Add(Context.ConnectionId, _mesa.NomeMesa);
Clients.Group(_mesa.NomeMesa).entrarMesa(jogador.Nome, posicaoMesa);
}
if (_mesa.QuantidadeVagas == 0)
{
IniciarJogo();
}
}
else
{
// ja estava na mesa (reconectando)
await Groups.Add(Context.ConnectionId, _mesa.NomeMesa);
if (jogadorServer != null)
posicaoMesa = _mesa.Jogadores.IndexOf(jogadorServer) + 1;
}
return posicaoMesa;
}
private void IniciarJogo()
{
EnviaCartas(true);
_mesa.EstaJogando = true;
}
.
.
.
.
}
Any suggestions?
Thanks