Код: Выделить всё
class instrument {
constructor(id, name, model, ws_connection) {
super(id, name, model);
this.connection_point = ws_connection;
this.extra_info = {
isConnected: false
}
}
async connect() {
this.socket = new websocket('ws://'+this.connection_point+':6402');
this.socket.onmessage = this.onMessage.bind(this);
this.socket.onopen = this.onOpen.bind(this);
this.socket.onclose = this.onClose.bind(this);
this.socket.onerror = this.onError.bind(this);
await new Promise((resolve, reject) => {
this.socket.onopen = resolve;
this.socket.onerror = reject;
});
this.extra_info.isConnected = true;
}
onMessage(event) {
//Code that handles data
}
onOpen(event) {
this.extra_info.isConnected = true;
}
onClose(event) {
this.extra_info.isConnected = false;
}
onError(event) {
console.log(event);
}
//Other code here handles the data processing
}
< /code>
Я понял, что, работая на моей приборной панели, если у меня есть один инструмент, у меня нет проблем. Однако, когда я пытаюсь подключить два инструмента, второй инструмент зацикливается.const websocket = require('ws');
const instrument = require("./api/instrumentClass");
const myInstrument1 = new instrument(1, 'test', 'test', '1.1.1.1');
myInstrument1.connect().then(e => {
console.log("1: Connected");
}).catch((e) => {
console.log("1: Error");
});
const myInstrument2 = new instrument(1, 'test', 'test', '2.2.2.2');
myInstrument2.connect().then(e => {
console.log("2: Connected");
}).catch((e) => {
console.log("2: Error");
});
Подробнее здесь: https://stackoverflow.com/questions/793 ... necting-pr