【问题标题】:I want to work with live chat in SignalR and AngularJS我想在 SignalR 和 AngularJS 中进行实时聊天
【发布时间】:2017-04-30 08:05:10
【问题描述】:

尝试使用SignalR 发送消息时出现以下错误:

TypeError: $scope.chatHub.server.SendMessageToAdmin 不是函数

这个函数出现错误:

vm.sentUserMsg = function (message) {
    var userName = 'Guest';
    $scope.name = 'Guest'; // holds the user's name
    $scope.message = ''; // holds the new message
    $scope.messages = []; // collection of messages coming from server
    $scope.chatHub = null; // holds the reference to hub
    $scope.chatHub = $.connection.chatHub; // initializes hub
    $.connection.hub.start(); // starts hub
    $scope.chatHub.client.broadcastMessage = function (name, message) {
        var newMessage = name + ' says: ' + message;
        $scope.messages.push(newMessage);
        $scope.$apply();
    };
    var producttype = '1';
    $scope.chatHub.server.SendMessageToAdmin(userName, message, producttype);
    $scope.message = '';
}

【问题讨论】:

    标签: javascript signalr


    【解决方案1】:

    我会冒险猜测您的方法“SendMessageToAdmin”存在大小写问题。

    我假设您遵循通用约定,并且位于 Hub 类中的服务器上的 SendMessageToAdmin 方法以大写“S”开头。请注意,当 SignalR 生成 Javascript 代理时,它会更改方法名称的大小写以匹配 Javascript 约定。因此,您的 javascript 代码中的方法名称应为驼峰式。示例:

    $scope.chatHub.server.sendMessageToAdmin(userName, message, producttype);
    

    相关文档是here

    具体来说,引用如下:

    在 Hub 上传入方法名称的驼峰式版本。信号R 自动进行此更改,以便 JavaScript 代码可以符合 JavaScript 约定。

    【讨论】:

      猜你喜欢
      • 2018-09-25
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多