В моем случае у меня есть две вкладки, и я тренируюсь, чтобы отправить сообщение от двух из них.Проблема здесь в том, что когда я посылаю socket.push, он работает на стороне сервера, но request.OnMessage не работает.Здесь подробности.
Первые вкладки:
var request = new atmosphere.AtmosphereRequest();
var transport = 'sse';
// We are now ready to cut the request
request = { url:'/cockpit',
logLevel: 'debug',
contentType : "application/json",
trackMessageLength : true,
shared : true,
'enableXDR': true,
transport : transport};
var socket = atmosphereService.subscribe(request);
console.log(socket);
console.log(request);
socket.push(JSON.stringify({from: 'Deep Thought', content: 'What is the answer to life the universe and everything?'}));
request.OnOpen = function(response) {
console.log("On Open request work ! ");
};
//onMessage is triggered when Atmosphere server sends an asynchronous message to the browser.
request.OnMessage =function(response){
console.log("On Message request work ! ");
console.log(response);
var obj = JSON.parse(response);
console.log(obj);
if(obj.content==="id_card_closed"&&sessionId===obj.from){
//Do something ...
};
Вторые вкладки:
this.changeProcessedStatus = function (sup) {
serviceCockpit.loadData([urlSupplierWarningByRefDetailProcessedUpdate], sup).then(function (results) {
});
serviceCockpit.loadData([urlSupplierWarningByRefProcessedStatus], params).then(function (results) {
self.globalWarning = results[0].data;
});
var request = new atmosphere.AtmosphereRequest();
var transport = 'sse';
request = {
url: '/cockpit',
logLevel: 'debug',
contentType: "application/json",
trackMessageLength: true,
shared: true,
'enableXDR': true,
transport: transport,
fallbackTransport: 'long-polling'
};
var socket = atmosphereService.subscribe(request);
console.log(socket);
//socket is used to push messages to the server.
var sessionId = session.session.idSession;
console.log(sessionId);
socket.push(JSON.stringify({from: sessionId, content: 'id_card_closed'}));
/*
var host = $location.host();
var port = $location.port();
var myContextWithPath = $window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/"));
console.log(myContextWithPath);
var socket = new WebSocket("ws://"+host+":"+port+myContextWithPath+"/ws/idcard");
socket.onopen=function (event) {
var data = { "from" : sessionId,"content" : "id_card_closed" };
var message = JSON.stringify(data);
socket.send(message);
};
*/
};