User-1854646908 posted
Hi guys,
I want to call SignalR Hub method from AJAX Action in Controller, everything seem no problem, but nothing happen when execute code, and no error appear,
I put the signalr-jquery in layout page,
that is all my code
Layout Page:
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>@ViewBag.Title</title>
</head>
<body>
@RenderBody()
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script>
<script src="/signalr/hubs"></script>
</body>
</html>
AJAX Action in Chat Controller:
public class ChatController : BaseController
{
[HttpPost]
public async Task<ActionResult> AddChat(ChatVM model)
{
var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
await context.Clients.All.receiveMessage(model);
// some code here
return PartialView("_Comment", chatList);
}
}
Chat Hub:
public class ChatHub : Hub
{
}
Javascript Code in Chat page:
var onReceive = function (obj) {
alert("work right");
}
window.addEventListener('load', function () {
var chatHub;
chatHub = $.connection.chatHub;
chatHub.client.receiveMessage = onReceive;
$.connection.hub.start();
}, false);