2.2.6.Creating a client method and calling from server

  • 1.Modify the server method: call all the clients in server method

[HubName("myhub")]
public class MyHub:Hub
{
    public void serverMethod(string msg)
    {
        Clients.All.clientMethod(msg + " good morning");
        //return msg + " good morning";
    }
    public MyHub()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}
  • 2.Modify html file: remove the action if server method success

<script>
    $(document).ready(function () {
        var obj = $.connection.myhub;
        obj.client.clientMethod = function (data) {
            alert(data);
        }

        $.connection.hub.start().done(function () {
            obj.server.serverMethod("Hi");
        });
    });
</script>
  • 3.Open few client and refresh one of them

    • All client will throw alert message

Last updated