Express node js webssockets is recieving messages from websocket server but not being able to send them

社会主义新天地 提交于 2020-12-13 05:40:36

问题


I have websockets setup with aws api gateway, I also have an ec2 instance running with node and express and ws websockets, I am able to send a message with wscat to the apigateway wss url and it shows up in the ec2 instance, but if I send a message from the ec2 instance it doesnt show up back in the wscat. I followed this tut to setup the apigateway https://aws.amazon.com/blogs/compute/announcing-websocket-apis-in-amazon-api-gateway/

Here is the ec2 instance code. I am not quite sure what might be wrong with it

const express = require("express");
const app = express();
const port = 8080;
const WebSocket = require("ws");
const url = "wss://obsf.execute-api.us-east-1.amazonaws.com/Prod";
const ws = new WebSocket(url);

process.setMaxListeners(0) // I have tried with and without this line

app.get("/", (req, res) => {
  var test = { action: "sendmessage", data: "hello world" };

  ws.on("open", function open() {
    ws.send(JSON.stringify(test));
  });

  ws.on("message", function incoming(data) {
    console.log(data);
  });

  ws.on('error', (error) => {
    console.log(error)
  });

  res.send("Hello World!");
});

app.post("/doorbell", (req, res) => {
  console.log(req);
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

Update:

I am actually getting the following errors

0|express  |     at Module.load (node:internal/modules/cjs/loader:948:32)
0|express  |     at Function.Module._load (node:internal/modules/cjs/loader:789:14)
0|express  |     at Object.<anonymous> (/home/ubuntu/.nvm/versions/node/v15.1.0/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
0|express  |     at Module._compile (node:internal/modules/cjs/loader:1083:30)
0|express  |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
0|express  |     at Module.load (node:internal/modules/cjs/loader:948:32)
0|express  |     at Function.Module._load (node:internal/modules/cjs/loader:789:14)
0|express  | (node:13300) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 open listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit
0|express  | (Use `node --trace-warnings ...` to show where the warning was created)
0|express  | (node:13300) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit
0|express  | (node:13300) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit
0|express  | (node:13392) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 open listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit
0|express  | (Use `node --trace-warnings ...` to show where the warning was created)
0|express  | (node:13392) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit
0|express  | (node:13392) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit

Update I ahve also tried adding the following

var EventEmitter = require('events');


const ee = new EventEmitter()
ee.setMaxListeners(0);

and still getting the eventEmitter error

Update:

So it works if I tak the ws.on("open")... out of the app.get, I actually want to put in in the post will I get the same error in the post. I guess I wll test that, but what gives why cant I put it in the apt.get

Update: I tried putting in in the post request but I still get the event emitter error.


回答1:


I got an aswer from the ws github issues, so I am posting it for anyone else

app.post('/doorbell', (req, res) => {
  var hmm1 = { action: 'sendmessage', data: 'hello world from post request' };

  if (ws.readyState === WebSocket.OPEN) {
    ws.send(JSON.stringify(hmm1));
    res.send('heya hey');
  } else {
    res.send('The WebSocket connection is not open');
  }
});


来源:https://stackoverflow.com/questions/64868510/express-node-js-webssockets-is-recieving-messages-from-websocket-server-but-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!