如何将websocket变量传递给控制器,以便通过相同的websocket连接发回数据?
app.factory('MyData', function($websocket, $q){
// Open a WebSocket connection
var dataStream = $websocket('ws://address');
var collection = [];
dataStream.onMessage(function(message) {
var data = collection.push(JSON.parse(message.data));
var data = (JSON.stringify(collection));
var data = Object(collection);
// Check for requests to Delete posted Questions.
for (var i = 0; i < data.length; i++) {
var uuid1 = collection[i].uuid;
// console.log(uuid1);
for (var j = 0; j < data.length; j++) {
var uuid2 = collection[j].uuid;
// console.log(uuid2);
if (i !=j){
if (uuid1 == uuid2){
console.log("Delete Question Request for uuid: " + uuid1);
collection.splice(i);
collection.splice(j);
}
else console.log("No Questions to Delete");
}
}
}
});
//Function that returns data from websocket and parses into JSON Array
var methods = {
collection: collection,
get: function() {
dataStream.send({ action: 'get' });
}
};
return methods;
});
我的控制器有函数joinChatRoom(),我试图通过相同的开放websocket连接发回数据。:
app.controller('WaitingRoom', function ($scope, $websocket, MyData) {
$scope.MyData = MyData;
console.log(MyData);
$scope.joinChatRoom = function(uuid){
var code = "code";
var uuidTitle = "uuid";
var chatRequest = {};
chatRequest[code] = 100;
chatRequest[uuidTitle] = uuid;
var sendChatRequest = JSON.stringify(chatRequest);
// send request to join chat room.
dataStream.send(sendChatRequest);
};
});
答案 0 :(得分:0)
我猜你在工厂里有某种获取数据的getter函数...... 您所要做的就是将WebSocket服务注入控制器的范围,您应该能够调用getter函数,其余部分将完成。 .controller(&#39;)
答案 1 :(得分:0)
您需要在致电:
时从控制器向您的工厂发送回调检查我对这个问题的回答:
Retrieving data out of post success from separate controller angularjs