我正在开发作为两个独立应用程序的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