我尝试使用Angular 1.5(使用ES6)创建服务或工厂,其中我可以拥有多个实例,每个实例都与WebSocket有不同的连接(其主要目的是聊天系统) )。
我能够为单个WebSocket连接提供服务,但考虑到这个项目的目的,我需要能够连接到不同的房间&#34;但是每个人都有一个具有不同连接参数的URL(如:ws://localhost:8080/chat/<param1>/<param2>
)。
我使用 angular-websocket (https://github.com/AngularClass/angular-websocket)。由于我使用严格模式的ES6,我必须从这个lib中注入$websocket
并在构造函数上立即创建它的实例。
所以,我正在寻找的是:能够创建多个WebSocket连接,理想情况是在服务/工厂中,其中每个连接都有自己的连接参数(将在控制器上给出服务将被实例化),然后每个实例将能够管理新的相应的房间的发送/接收&#34;消息。
使用ES5,我可能会创建一个非单身人士服务或工厂,这可能解决了这个问题,但是当我学习ES6时,我真的很想让这个解决。 这是我目前的聊天服务类,目前只能处理静态连接,而且它是一个单例。
export default class ChatService {
constructor($websocket) {
'ngInject';
this._$websocket = $websocket('wss://localhost:8080/chat' + '/param1/param2');
this.collection = [];
this.init();
}
init() {
this._$websocket.onMessage(this.onMessage);
this._$websocket.onOpen(this.onOpen);
this._$websocket.onClose(this.onClose);
this._$websocket.onError(this.onError);
}
onOpen() {
console.log('Connection open');
}
onClose(event) {
console.log('Connection closed: ', event);
}
onError(event) {
console.log('Connection Error: ', event);
}
onMessage(message) {
this.collection.push(JSON.parse(message.data));
}
closeSocket() {
this._$websocket.close();
}
sendMessage(text) {
// Code to send a message using this connection
}
}
如果您对如何解决这个问题有任何其他建议,我全心全意。
谢谢。
答案 0 :(得分:0)
我使用jquery atmosphere js进行多套接字连接。你可以使用它。
Exapmle多连接代码:
HTML
<button id="b1">
ping s1
</button>
<button id="b2">
ping s1
</button>
<div id="s1">
<h1>s1 pings</h1>
<p></p>
</div>
<div id="s2">
<h1>s2 pings</h1>
<p></p>
</div>
JS:
var container;
function Request(sU) {
this.url = sU;
this.contentType = 'application/json';
this.logLevel = 'debug';
this.trackMessageLength = false;
this.enableProtocol = false;
this.enableXDR = true;
this.transport = 'websocket';
this.fallbackTransport = 'sse';
this.reconnectInterval = 5000;
this.connectTimeout = 30000;
this.timeout = 60000;
this.maxReconnectOnClose = 3;
this.isDetail = false;
this.suspend = true;
//this.headers = {device: $rootScope.imageType}
this.onOpen = function(response) {
console.log("connected");
};
this.onReopen = function(response) {};
this.onMessage = function(response) {
$(container).append("<br>" + response.responseBody );
console.log(response)
};
this.onClientTimeout = function(request) {};
this.onReconnect = function(request, response) {};
this.onError = function(response) {};
this.onClose = function(response) {};
};// Request
function SocketConnector(sU) {
return {
request: new Request(sU),
socket: null,
closeSocket: function(aciklama) {
this.socket.close();
}, //closeSocket
connectSocket: function() {
this.socket = $.atmosphere.subscribe(this.request);
} //connectSocket
};
};
var socket1 = new SocketConnector('https://echo.websocket.org');
socket1.connectSocket();
$("#b1").click(function(){
container = "#s1";
socket1.socket.push(Date.now())
});
var socket2 = new SocketConnector('https://echo.websocket.org');
socket2.connectSocket();
$("#b2").click(function(){
container = "#s2";
socket2.socket.push(Date.now())
});
您可以在任何角度服务js中写这个。