我想请求你的帮助。 我有:
main.js
angular.module('app',['btford.socket-io'])
.factory('socket', function (socketFactory) {
var myIoSocket = io.connect('http://localhost');
var mySocket = socketFactory({
ioSocket: myIoSocket
});
return mySocket;
})
.directive('myDir',function(){
return {
restrict: 'E',
scope:{
marker:'@'
},
template: '<p ng-bind="marker"></p><ul><li ng-repeat="item in ItemList"><div ng-bind="item"></div></li></ul>',
controller:function($scope, socket){
socket.emit('joinRoom', $scope.marker );
socket.on('updateEvent',function(data){
$scope.ItemList= data;
});
}
};
})
.controller('main',function($scope, $http){
$http.get('localhost/api/getList')
.success(function(data){
$scope.markerlist = data;
});
});
的index.html
<html lang="en">
<body ng-app="app">
<div ng-controller='main' class="main">
<div ng-repeat='marker in markerlist'>
<my-dir marker='{{marker}}'></my-dir>
</div>
</div>
</body>
<script src="./libs/socket.io-client/socket.io.js"></script>
<script src="./libs/angular/angular.min.js"></script>
<script src="./libs/angular-socket-io/socket.min.js"></script>
<script src="./js/main.js"></script>
</html>
工作流程如何运作:
“marker1”加入房间“marker1” “marker2”加入房间“marker2” ...
但在第6步:
然后io向房间发送数据,指令“marker1”,“marker2”,“marker3”接收相同的ItemList,全部在一起,
并且ItemList中的数据一直在重写
有什么建议吗? %) 谢谢!
答案 0 :(得分:0)
解决了
var myIoSocket = io.connect('http://localhost', {'force new connection': true});
文档: https://github.com/Automattic/socket.io/wiki/configuring-socket.io