Pregunta

Estoy usando signationr con asp.net 4.5 formas web. No pude hacer que el cliente hable con el servidor y viceversa. Lo que quiero lograr es simplemente poder probar cómo el cliente puede activar una función de servidor y cómo el servidor puede activar una función de cliente. Aquí está el código que utilizo:

Código del lado del cliente (Hitcounder.aspx)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script>
<script src="Scripts/jquery.signalR-0.5.3.min.js"></script>
<script type="text/javascript" src="SignalR/Hubs"></script>

<style>
    #currentHitCount {
        font-family: Arial;
        font-size:40pt;
        margin-left:auto;
        margin-right:auto;
        display:block;
        text-align:center;
    }

</style>
</head>
<body>
<div id="currentHitCount"></div>
<script type="text/javascript">

    $(function () {
       var hub = $.connection.hitCounter;

       $.extend(hub, {
           showHitCount: function (hitCount){
               if(hitCount > 1){
                   $('#currentHitCount')
                   .html("This site has had " + hitCount + " hits.");
               }
               else{
                   $('#currentHitCount')
                   .html("This site has had " + hitCount + " hit.");
               }
           },
           addMessage: function (str) {
               $('#currentHitCount')
               .html("Getting Message: " + str);
           }
       });

       $.connection.hub.start(function () {
           hub.addMessage("test");
           hub.addHit();

       });

       $.connection.hub.stateChanged(function (change) {
           if ($.signalR.connectionState["connected"] === change.newState) {                  
           }
       });

       $.connection.hub.error(function () {
       });

   });


   </script>
  <div style="background-color:red; width:290px; height:200px; color:white; position:absolute; top:100px; left:30px;" id="thebutton">test</div>

</body>
</html>

Código del lado del servidor (APP_CODE / HITCOUNTERHUB.CS)

using SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

[HubName("hitCounter")]
public class HitCounterHub : Hub, IDisconnect
{
    static int _hitCount;

public void addHit(string pageId)
{
    _hitCount += 1;
    Clients.showHitCount(_hitCount);

}

public Task ClientCallingServer()
{
    _hitCount += 1;
    return Clients["foo"].showHitCount(_hitCount);
}


public Task Join()
{
    return Groups.Add(Context.ConnectionId, "foo");
}

public Task Send(string message)
{
    _hitCount += 1;
    return Clients["foo"].addMessage(message);       
}


public Task Disconnect()
{
    return Clients["foo"].leave(Context.ConnectionId);
}
}

Estoy ejecutando el código localmente en IIS7.5, pero me recibo y se meta al ejecutar el código en Visual Studio 2012 también.

El error:

localhost / Signalr / Signalr / Sand? Transport= ServersentEvents & ConnectEID= 400F1302-6C8E-418E-A14C-DA95F836A29D 500 (error del servidor interno)

Uso del depurador de Chrome La página de error muestra: El método 'Addhit' no se pudo resolver.

de nuevo, lo que estoy tratando de hacer es hacer una prueba simple para verificar cómo llamar a un servidor desde el cliente y el cliente desde el servidor.

gracias.

¿Fue útil?

Solución

La razón por la que no puede resolver su método Addhit es porque tiene el mismo nombre de función en el servidor a medida que hace el cliente.

AKA para llamar al servidor que está haciendo HUB.ADDHIT ("LAKSJDF").Pero para llamar al cliente, sería lo mismo: hub.addhit ("laksjdfldksj");Por lo tanto, hay ambigüedad.Cambie el nombre de su cliente o funciones del lado del servidor para que sean únicos en su sentido de nombrar.

Este problema se fijará en la próxima versión de Signalr.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top