简体   繁体   English

HTML中的NodeJS Websocket使用变量(带有express?)

[英]NodeJS Websocket use variable in HTML (with express?)

I have been searching for two days now looking for a solution that might work for me. 我一直在搜索两天,以寻找可能适合我的解决方案。 Sadly I have only seen examples and guides on how to setup a websocket server (that sends messages back and forth to clients) and a websocket client (that resides in browser). 可悲的是,我仅看到了有关如何设置websocket服务器(将消息来回发送到客户端)和websocket客户端(驻留在浏览器中)的示例和指南。 None of these really work for me, and I am not sure how to achieve what I want here. 这些都不对我真正有用,我不确定如何在这里实现我想要的。

Basically I have the following websocket: 基本上我有以下websocket:

require('dotenv').config()

const WebSocket = require('ws');

var connection = new WebSocket('ws://XXX');

connection.onopen = function () {
  connection.send(JSON.stringify({"authenticate":process.env.API}));
  connection.send(JSON.stringify({"XXX":"YYY"}));
  connection.send(JSON.stringify({
    "db" : "unique_id", 
    "query" : { 
        "table" : "users"
    } 
}));  
};
connection.onerror = function (error) {
  console.log('WebSocket Error ' + error);
};

connection.onmessage = function (e) {
  console.log('Server: ' + e.data);
  var myResponse = JSON.parse(e.data);
  var qList = myResponse.results;
};

What I want to do is have my nodeJS-script running, for example an express script with a html page, that also includes the response from onmessage. 我想要做的是运行我的nodeJS脚本,例如带有html页面的快速脚本,其中还包括onmessage的响应。 Why I am complicating this instead of just using the websocket client-side is that I cannot send my auth-code publicly. 为什么我使此复杂而不是仅使用websocket客户端,是因为我无法公开发送身份验证代码。

Hope I have been clear enough, let me know if you are unsure of my question! 希望我已经足够清楚,如果您不确定我的问题,请告诉我!

PS. PS。 If you think I would be better off using another websocket-script such as Socket.io - I have been looking at them and have not gotten much wiser sadly. 如果您认为我最好使用Socket.io等其他websocket脚本-我一直在研究它们,可悲的是,我并没有变得更加明智。

You have a lot of options. 您有很多选择。 Probably the easiest is to export the connection. 可能最简单的方法是导出连接。 At the bottom of the file, eg module.exports = connection 在文件的底部,例如module.exports = connection

Then in the express application, import the connection, eg const connection = require('./path/connection'); 然后在快速应用程序中,导入连接,例如const connection = require('./path/connection');

Then make a function that calls itself at a given interval and sends the appropriate message. 然后,创建一个以给定间隔调用自身并发送适当消息的函数。

Then within the Express app you can use something like connection.on('message', (data, flags) => // do stuff); 然后在Express应用程序中,您可以使用诸如connection.on('message',(data,flags)=> //做事)之类的东西。

Your other option is to create a store object. 您的另一个选择是创建一个存储对象。 Eg 例如

// ./store.js

class store  {
  constructor() {
    this.wsMaterial = {};
  }

 add(wsInfo) {
    this.wsMaterial[key] = wsInfo

 }

 get store() {
   return this.wsMaterial
 }
}

module.exports = new store()

Then import the store and updated it, eg 然后导入商店并对其进行更新,例如

// ./websocket file

const store = require('./store');

...

connection.onmessage = function (e) {
  console.log('Server: ' + e.data);
  var myResponse = JSON.parse(e.data);
  var qList = myResponse.results;
  store.add(qList)
};

Then from Express... 然后从快递...

// ./express.js

const store = require('./store');

store.get // all of your stuff;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM