Angular 1.5:多个WebSocket连接
Angular 1.5: multiple WebSocket connections
我正在尝试使用 Angular 1.5(使用 ES6)创建一个服务或工厂,在那里我可以拥有它的多个实例,每个实例都与 WebSocket 有不同的连接(这个的主要目的是一个聊天系统)。
我能够做一个适用于单个 WebSocket 连接的服务,但考虑到这个项目的目的,我需要能够连接到不同的 "rooms",但它们每个都有一个 URL 具有不同的连接参数(例如:ws://localhost:8080/chat/<param1>/<param2>
)。
我正在使用 angular-websocket (https://github.com/AngularClass/angular-websocket)。因为我在严格模式下使用 ES6,所以我必须从这个库中注入 $websocket
并立即在构造函数上创建它的一个实例。
所以,我正在寻找的是:能够创建多个 WebSocket 连接,最好是在 service/factory 中,其中每个连接参数都有自己的连接参数(将在控制器上给出)此服务将在其中实例化),然后每个实例将能够管理 sending/receiving 个新的相应 "room" 消息。
使用 ES5,我可能会创建一个非单例服务或工厂,这可能会解决这个问题,但在我学习 ES6 时,我真的很想以这种方式解决这个问题。
这是我当前的聊天服务 class,目前只能处理静态连接,并且是单例。
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
}
}
如果您对如何解决这个问题有任何其他建议,我会洗耳恭听。
谢谢。
我使用 jquery atmosphere js 进行多套接字连接。你可以使用它。
示例多连接代码:
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())
});
你可以在任何 angular 服务 js 中写这个。
我正在尝试使用 Angular 1.5(使用 ES6)创建一个服务或工厂,在那里我可以拥有它的多个实例,每个实例都与 WebSocket 有不同的连接(这个的主要目的是一个聊天系统)。
我能够做一个适用于单个 WebSocket 连接的服务,但考虑到这个项目的目的,我需要能够连接到不同的 "rooms",但它们每个都有一个 URL 具有不同的连接参数(例如:ws://localhost:8080/chat/<param1>/<param2>
)。
我正在使用 angular-websocket (https://github.com/AngularClass/angular-websocket)。因为我在严格模式下使用 ES6,所以我必须从这个库中注入 $websocket
并立即在构造函数上创建它的一个实例。
所以,我正在寻找的是:能够创建多个 WebSocket 连接,最好是在 service/factory 中,其中每个连接参数都有自己的连接参数(将在控制器上给出)此服务将在其中实例化),然后每个实例将能够管理 sending/receiving 个新的相应 "room" 消息。
使用 ES5,我可能会创建一个非单例服务或工厂,这可能会解决这个问题,但在我学习 ES6 时,我真的很想以这种方式解决这个问题。 这是我当前的聊天服务 class,目前只能处理静态连接,并且是单例。
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
}
}
如果您对如何解决这个问题有任何其他建议,我会洗耳恭听。
谢谢。
我使用 jquery atmosphere js 进行多套接字连接。你可以使用它。
示例多连接代码:
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())
});
你可以在任何 angular 服务 js 中写这个。