Reputation: 69
I'm facing a strange issue, I'm able to call the server function but in return when I try to call the client function , it does does not call Am I doing something wrong or missing out some code or configuration to be done
I'm using Visual studio 2010 and developing a web app
the Server code just a simple class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRDemo
{
public class MyHub:Hub
{
public void GetMessage(string msg)
{
Clients.All.getMsg1("Got :"+msg);
}
}
}`
in global asax I have
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
}
Client Side code I just click the btn , it calls the server function GetMessage as expected but in return it does not call getMsg1
$(document).ready(function () {
debugger;
var chat = $.connection.myHub;
chat.getMsg1 = function (msg) {
debugger;
alert(msg);
};
$("#btn").click(function () {
chat.server.getMessage("called");
});
$.connection.hub.start()
});
I googled I found that we need to register the client function before starting the hub conn which I did, but still it does not work nor it raises any error
Upvotes: 1
Views: 2333
Reputation: 18311
The reason why you're not having your client side function executed is because your binding it wrong.
Change:
chat.getMsg1 = function (msg) {
debugger;
alert(msg);
};
to
chat.client.getMsg1 = function (msg) {
debugger;
alert(msg);
};
Also one other thing to note as a good practice is you should always bind your event handlers one the connection has fully started. So you can write your client code as follows:
$(document).ready(function () {
debugger;
var chat = $.connection.myHub;
chat.client.getMsg1 = function (msg) {
debugger;
alert(msg);
};
$.connection.hub.start().done(function() {
$("#btn").click(function () {
chat.server.getMessage("called");
});
});
});
Upvotes: 4