问题
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