I have created a sample signalR for POC. I want to call a hub method from Global.asax and pass a string value to client. My Message hub is :-
[HubName("messageHub")]
public class MessageHub : Hub
{
public static IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
public void Message()
{
/*
* services and updates the messages
* property on the PushMessage hub
*/
//IHubContext context = GlobalHost.ConnectionManager.GetHubContext<SignalR_Error_Logging.Models.ErrorModel>();
List<GenerateError.Repository.ErrorModel> model = ErrorRepository.GetError();
context.Clients.pushMessages(model[0].ErrorMessage);
}
I have defined two of the scripts in layout.cshtml
<script type="text/javascript" src="../../Scripts/jquery-1.6.4.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.signalR-0.5.3.js"></script>
My Index.html is as below:-
@{
ViewBag.Title = "Receive Error message";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var myHub = $.connection.messageHub;
myHub.pushMessages = function (value) {
console.log('Server called addMessage(' + value + ')');
$("#messages").append("<li>" + value + "</li>");
};
$("#btnMessage").click(function () {
myHub.message();
});
$.connection.hub.start().done(function () { alert("Now connected!"); }).fail(function () { alert("Could not Connect!"); });
});
</script>
<h2>Receive Error Messages</h2>
<ul id="messages"></ul>
<input type="button" id="btnMessage" value="Get Error" />
In Global.asax I have written
SignalR_Error_Logging.SignalRHub.MessageHub hub = new SignalRHub.MessageHub();
hub.Message();
In Application_Start();
I am not able to display message in my UI(i.e Index.cshtml).
Things that i have tried:-
- Running the application as IIS.
Changing the way of creating HubContext.
IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>(); context.Clients.notify("Hello world");
if (Clients != null) { Clients.shootErrorMessage(message); this.Clients.shootErrorMessage(message); }
Gone thru links of StackoverflowCalling SignalR hub clients from elsewhere in system
Any advice???
When i call my hub method by creating a button in Index.html it works fine.
Apologies for not framing my question properly!!