在另一个Websocket连接中发送Websocket数据



我们有一个loxone服务器,它通过loxone websocket (https://github.com/Loxone/lxcommunicator)向我们的web服务器发送数据。然后,该web服务器也通过websocket将数据发送给客户端。我们之所以选择这种设置,是因为我们只有一个到loxone服务器本身的连接,并且只需要进行一次身份验证。

现在的问题是,从loxone服务器接收事件的函数必须在到loxone服务器的连接开始之前在配置变量中声明。然而,在这个作用域(socketOnEventReceived)中,我们没有连接到客户端的websocket。

我们也不能在loxone-socket定义周围添加client-websocket,因为这样会为每个客户端创建一个新的loxone-websocket连接。

这是我们当前的代码(ws/wss = client-socket, socket = loxone-socket)——这种方式的问题是,它为每个接收到的事件创建一个新的websocket(显然也不可行)。

if (typeof LxCommunicator === 'undefined') {
global.LxCommunicator = require('lxcommunicator');
}
//=== Node.js only ===
var fs = require('fs');
var WebSocket = require('ws');
var privateKey = ; //PkeyFile
var certificate = ; //CertFile
var credentials = { key: privateKey, cert: certificate };
var https = require('https');
var httpsServer = https.createServer(credentials);
httpsServer.listen(60088);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: httpsServer
});

// Prepare our variables
// uuid is used to identify a device
var uuid = getUUID(),
// delegateObj is contains all available delegate methods
delegateObj = {
socketOnDataProgress: function socketOnDataProgress(socket, progress) {
//console.log(progress);
},
socketOnTokenConfirmed: function socketOnTokenConfirmed(socket, response) {
//console.log(response);
},
socketOnTokenReceived: function socketOnTokenReceived(socket, result) {
//console.log(result);
},
socketOnTokenRefresh: function socketOnTokenRefresh(socket, newTkObj) {
//console.log(newTkObj);
},
socketOnConnectionClosed: function socketOnConnectionClosed(socket, code) {
process.exit(-1);
},
socketOnEventReceived: function socketOnEventReceived(socket, events, type) {
events.forEach(function(event) {
if(type === 2 || type ===3){ //2=lichtstatus 3=moodstatus
var data = {};
data["uuid"] = event.uuid;
data["value"] = event.value;
data['text'] = event.text;
wss.on('connection', ws => {
ws.send(JSON.stringify(data));
})
}
});
}
},
// deviceInfo is a device specific information like the userAgent of a Browser
deviceInfo;
// Node.js doesn't have a userAgent, lets use the hostname instead
if (typeof window !== "undefined") {
deviceInfo = window.navigator.userAgent;
} else {
deviceInfo = require('os').hostname();
}
// OPTIONAL
// If no version is set LxCommunicator.WebSocket will fetch the version on its own
// This version is needed to determine if the Miniserver supports encryption and tokens
//LxCommunicator.setConfigVersion("9.3.2.20");
// Get the LxCommunicator.WebSocketConfig constructor, to save some space
var WebSocketConfig = LxCommunicator.WebSocketConfig;
// Instantiate a config object to pass it to the LxCommunicator.WebSocket later
var config = new WebSocketConfig(WebSocketConfig.protocol.WS, uuid, deviceInfo, WebSocketConfig.permission.APP, false);
// OPTIONAL: assign the delegateObj to be able to react on delegate calls
config.delegate = delegateObj;
// Instantiate the LxCommunicator.WebSocket, it is our actual WebSocket
var socket = new LxCommunicator.WebSocket(config);
// Open a Websocket connection to a miniserver by just providing the host, username and password!
socket.open("loxoneserver", "loxoneuser", "loxonepassword").then(function() {
socket.send("jdev/sps/enablebinstatusupdate").then(function(respons) {
console.log("Successfully executed '" + respons.LL.control + "' with code " + respons.LL.Code + " and value " + respons.LL.value);
}, function(err) {
console.error(err);
process.exit(-1);
});
}, function(e) {
console.error(e);
});

保留所有连接的索引,然后使用可用的连接。

let connections = {};
wss.on('connection', ws => {
//Keep an index for incoming connections
const id = Math.random().toString(36).substr(2, 8); 
connections[id] = ws;
//remove once its closed.
ws.on("close",()=>{
delete connections[id]
})
})

然后像下面这样更新你的方法。

socketOnEventReceived: (socket, events, type) => {
events.forEach(function(event) {
if(type === 2 || type ===3){ //2=lichtstatus 3=moodstatus
var data = {};
data["uuid"] = event.uuid;
data["value"] = event.value;
data['text'] = event.text;
Object.values(connections).forEach((conn)=>{
conn.send(JSON.stringify(data))
})
// wss.on('connection', ws => {
//     ws.send(JSON.stringify(data));
// })
}
});
}

因为我这边不能运行你的代码。我不能说100%有效。

创建集中式数据存储&共享数据,以下三个选项

  • 单例JS对象
  • <
  • RxJS主题/gh>
  • 回来的

相关内容

  • 没有找到相关文章

最新更新