在我的asp.net mvc web应用程序中,我正在使用angularjs signalr和requirejs ..
我在客户端创建了一个angularjs服务来设置signalr,我从angularjs控制器调用它来向signalr服务器发送消息,
服务器回调客户端方法,angularjs更新ui。
当我在剃刀页面的脚本标签中包含所有angularjs代码和信号器客户端时,这是有效的
但是当我合并requirejs时,我发送消息时会调用signalr服务器方法但是从未从服务器调用客户端函数以发送回客户端
意味着'acceptGreet'代理的好事永远不会被触发(请参阅代码) 我已经找到了解决方案,我希望我很清楚,这是我的代码,谢谢
define(["app/js/modules/appModule"], function (app) {
var service = app.factory('feedService', function ($rootScope) {
return {
proxy: null,
initialize: function (acceptGreetCallback) {
//Getting the connection object
connection = $.hubConnection();
//Creating proxy
this.proxy = connection.createHubProxy('feeds');
//Starting connection
connection.start(function () {
console.log('started..', this.proxy)
});
//Attaching a callback to handle acceptGreet client call
this.proxy.on('acceptGreet', function (message) {
$rootScope.$apply(function () {
acceptGreetCallback(message);
});
});
},
sendRequest: function (message) {
//Invoking greetAll method defined in hub
this.proxy.invoke('greetAll', message);
}
}
});
return service;})
//以下控制器消耗服务
define([app/js/services/feeds/feedService'], function () {
var controller = function ($scope, feedSssssexrvice) {
$scope.text = "";
$scope.message = [];
$scope.greetAll = function () {
feesService.sendRequest($scope.text);
}
updateGreetingMessage=function(text)
{
$scope.message.push(text);
}
feedService.initialize(updateGreetingMessage);
}
return controller;})
// REQUIREJS CONFIG
require.config({
baseUrl: "/Client/"
, paths: {
"jquery": "libs/jquery-1.10.2.min"
, 'signalr': 'libs/signalr/jquery.signalR-2.1.2.min'
},
shim: {
"angular": {
exports: "angular"
, deps: ["anglarFileUploadShim"]
}
, 'signalr': {
deps: ['jquery']
}
}
});
require(['jquery', 'bootstrap', 'signalr', 'app/feeds/js/bootstrap'], function ($) {
});
// signalr server
[HubName("feeds")]
public class FeedHub:Hub
{
public void GreetAll(string text)
{
Clients.All.acceptGreet(text);
}
}