就我而言,我有两个选项卡,我正在训练从其中两个选项卡发送消息。这里的问题是,当我发送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);
};
*/
};