我正在构建一个基于web-socket PUSH的atmosphere.js通用API。我想宣传申请。我写了下面的代码,如下所示:
function PushConnection() {
'use strict';
this.socket = atmosphere;
this.request = new atmosphere.AtmosphereRequest();
this.request.url = 'wss://echo.websocket.org';
this.request.contentType = 'application/json';
this.request.transport= 'websocket';
this.request.reconnectInterval= 5000;
this.request.timeout= 60000;
this.subSocket = this.socket.subscribe(this.request);
}
PushConnection.prototype.subscribe = function(data) {
'use strict';
var self = this;
var promise = new Promise(function(resolve, reject) {
self.request.onMessage = function(response) {
if (response.responseBody === '') {
resolve('No Result.');
} else {
var responseBody = response.responseBody;
console.log(responseBody);
resolve(responseBody);
}
};
self.request.onError = function(response) {
reject(response.responseBody);
};
self.subSocket.push(data);
});
return promise;
};
但是当我在浏览器中打开时,应用程序会抛出以下错误:“Websocket Not Connected”。请在下面找到堆栈跟踪:
log @ atmosphere.min.js:1
error @ atmosphere.min.js:1
X @ atmosphere.min.js:1
u @ atmosphere.min.js:1
AtmosphereRequest.push @ atmosphere.min.js:1
(anonymous function) @ pushPromise.js:32
PushConnection.subscribe @ pushPromise.js:17
提前致谢。