我正在開發作爲兩個獨立應用程序的Web應用程序,後端使用webapi c#和使用AngularJS的用戶界面。我只是想在這個項目中添加聊天選項。我已經安裝了SignalR並在webapi中添加了ChatHub.cs類。如何連接signalR from angularJs
中的WebAPI有一個名爲Startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
WebApiConfig.Register(config);
app.UseCors(CorsOptions.AllowAll);
ConfigureAuth(app);
app.UseWebApi(config);
app.MapSignalR();//added after installation of SignalR package
}
}
ChatHub類
public class ChatHub : Hub
{
public static string emailIDLoaded = "";
public void Connect(string userName, string email)
{
emailIDLoaded = email;
var id = Context.ConnectionId;
using (SmartCampEntities dc = new SmartCampEntities())
{
var userdetails = new ChatUserDetail
{
ConnectionId = id,
UserName = userName,
EmailID = email
};
dc.ChatUserDetails.Add(userdetails);
dc.SaveChanges();
}
}
}
不管請求我從用戶界面它將擊中到其相應的控制器在發送類的WebAPI。例如
$http({
method: 'GET',
url: $scope.appPath + "DashboardNew/staffSummary" //[RoutePrefix]/[Route]
}).success(function (result, status) {
data = result;
});
我的用戶界面是一個單獨的應用程序。我如何從UI連接signalR。 我嘗試了一些東西,但沒有得到它的工作。任何人都可以建議我如何得到它的工作
HTML代碼
<div>
<a class="btn btn-blue" ng-click="sendTask()">SendTask</a>
的JavaScript
angular.module('WebUI').controller('DashboardCtrl', function ($scope, $window, $http, $modal, ngTableParams) {
$scope.header = "Chat";
$scope.sendTask = function() {
$http({
method: 'POST',
url: $scope.appPath + hubConnetion.server.sendTask("userName","email"),
})
}
});
這與從網頁定期使用SignalR沒有區別。文檔很好,並且有很多關於如何使用它的例子。 –
可以請你分享任何例子或鏈接 – Boopathi
你真的需要什麼,或者更好,你需要什麼樣的失敗幫助並不那麼清楚。你想知道如何在Angular中集成Signalr的啓動,或者你正在失敗,例如CORS? – Hoghweed