无法获得angular-socket-io来显示消息

时间:2016-01-09 22:25:57

标签: angularjs socket.io

我的app.js文件中有以下声明:

(function () {
    angular.module('medmart', [
        'oc.lazyLoad',
        'ui.bootstrap', 
        'ngFileUpload',
        'ui.validate',
        'btford.socket-io',
    ]).
    factory('mySocket', function (socketFactory) {
        var mySocket = socketFactory({
            prefix: 'medmart~',
            ioSocket: io.connect('https://:::8443'),
        });
        mySocket.forward('error');
        return mySocket;
    });
})();

在我的index.html中,我有以下声明:

<script src="js/angular/angular.min.js"></script> <script src="js/plugins/oclazyload/dist/ocLazyLoad.min.js"></script> <script src="js/ui-router/angular-ui-router.min.js"></script> <script src="js/bootstrap/ui-bootstrap-tpls-0.12.0.min.js"></script> <script src="js/plugins/ng-file-upload/ng-file-upload.min.js"></script>
<script src="/socket.io/socket.io.js"></script> <script src="js/plugins/angular-socket-io/socket.js"></script>

在controlers.js中我有以下内容:

mySocket.forward('medmart~procedure', $scope);

$scope.user = {
    fname: '',
    lname: '',
    pid: ''
};
$scope.setCredentials = function() {    
    $scope.user.fname   = $rootScope.user.fname;
    $scope.user.lname   = $rootScope.user.lname;
    $scope.user.pid     = $rootScope.user.pid;
};
$scope.setCredentials();

mySocket.on('connect', function () {

    $scope.$on('socket:error', function (ev, data) {
        console.log("ERROR: " + data)
    });
    $scope.$on('medmart~procedure', function(event, data) {

        if (!data.payload) {
            return;
        } 
        $scope.$apply(function() {

        });
    });
});

在我的routs.js(Node.js)中,我发送以下内容: socket.emit('medmart~procedure', procedure);

但是,我无法在控制器中收到任何消息。有人可以告诉我,我做错了什么吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我终于找到了问题所在。而不是

$scope.$on('procedure:new', function(data){...});

它应该是:

mySocket.on('procedure:new', function(data){...});

我能够通过这个演示来解决这个问题: https://github.com/btford/angular-socket-io-im