在不带socket.io的弹性beantalk ALB上具有websockets的node.js服务器

我试图使用应用程序负载平衡器(ALB)在弹性beantalk(EB)中使用websockets使node.js服务器(使用express)工作,但不使用socket.io(因为peerjs-server是我的服务器)我试图开始运行,但未使用socket.io编写。)

我看过几篇文章,建议您必须使用socket.io(或另一个不仅仅依赖于websockets的库),但是亚马逊表示ALB直接支持websockets。

我的服务器既是create-react-app服务器又是peerjs服务器。对于Web UI和peerjs ws连接,它都可以在9000端口的dev中正常运行。

我已经尝试了所有发现的所有不同方法,但是我还没有做到这一点,我什至还看到写过一些东西表明它无法完成,但是似乎我米关闭。有没有人让它起作用,如果可以,怎么办?

yanyantianlong 回答:在不带socket.io的弹性beantalk ALB上具有websockets的node.js服务器

好的,我知道了。这就是我要使所有端口9000都能正常工作的方法。

在EB中,创建一个应用程序,然后开始创建环境。

在环境配置中,进入软件部分,并告诉您您将使用npm run prod来启动服务器。

enter image description here

现在,进入负载均衡器部分,如下图所示:

  1. 在端口9000上添加侦听器
  2. 在端口9000上创建一个进程并启用粘性(我称为mine peerjsServer)
  3. 为要用于访问服务器的每个URL路径添加规则,并将每个规则分配给您创建的进程(peerjsServer)。另外,将默认值指向该过程,以便对80进行运行状况检查成功到达您的服务器

enter image description here

您可能需要跳到AWS UI中的 EC2仪表板,以确保在那里定义了必要的安全组。我想我在上创建了前两个,最后两个是默认创建,但我不记得了。无论如何,他们需要为入站和出站打开端口9000(默认情况下始终始终存在端口80): enter image description here

返回 EB配置,转到实例部分,并确保为您的实例分配了安全组: enter image description here

我运行react-scripts build来创建包含服务器UI生产版本的/build目录(这是我在此不介绍的create-react-apps内容)。

在我的代码中,我使用server.js启动服务器,该服务器使服务器同时运行peerjs-server ws服务器和http服务器。

const express = require("express");
const bodyParser = require("body-parser");
const path = require('path');

const passport = require("passport");
const users = require("./routes/api/users");
const games = require("./routes/api/games");
const Game = require("./src/models/Game");

const WebSocket = require('ws');
const ExpressPeerServer = require('peerjs-server').ExpressPeerServer;

const app = express();
const url = require('url');

const port = process.env.PEERSERVERPORT || 9000; 

// WebSocket for making db updates to client.
const wsserver = require('./wsserver').wsserver

// Server that gets all requests: lobby UI,peerserver,db websocket
const server = require('http').createServer(app);

wsserver.on('connection',function connection(ws) {
  ws.on('message',msg => {
    ws.send(msg)
  })

  ws.on('close',() => {
    console.log('WebSocket was closed')
  })
});

server.on('upgrade',function upgrade(request,socket,head) {
  const pathname = url.parse(request.url).pathname;

  if (pathname === '/ws') {
    wsserver.handleUpgrade(request,head,function done(ws) {
      wsserver.emit('connection',ws,request);
    });    
  }
});

// Bodyparser middleware
app.use(
  bodyParser.urlencoded({
    extended: false
  })
);
app.use(bodyParser.json());

// Passport middleware
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);

// Routes -- the /api/* path is defined in the EB load balancer
app.use("/api/users",users);
app.use("/api/games",games);

// This is the create-react-apps /build directory of UI code
app.use(express.static(path.join(__dirname,'build')));
app.get('/login',function (req,res) {
  res.sendFile(path.join(__dirname,'build','index.html'));
});

// These are the paths that are defined in the EB load balancer
app.get('/logout','index.html'));
});
app.get('/register','index.html'));
});
app.get('/dashboard','index.html'));
});

// Peer server for making WebRTC connections between game clients.
const options = { 
    debug: true
}
const peerserver = ExpressPeerServer(server,options);
app.use('/peerserver',peerserver);

app.use(express.static(path.join(__dirname,'src')));

app.get('*',function(req,'src','index.html'));
});

peerserver.on('disconnect',(client) => {
  console.log('Delete the game if the host client disconnects');
  Game.deleteMany({ hostPeerId: client })
    .then(() => {
      wsserver.clients.forEach(function each(client) {
        if (client.readyState === WebSocket.OPEN) {
          client.send("refreshGames");
        }
      })
    })
    .catch(err => console.log(err));
});

server.listen( port,() => console.log(`Server is up and running on port ${port} !`))

然后在我的package.json中,为prod设置脚本以运行上述server.js来启动服务器,并在前面的 Software 部分中放入npm run prod是什么叫它使服务器运行:

  ...
  "scripts": {
    "start": "set PORT=8081&& react-scripts start","prod": "node server.js",...

完成所有操作后,我现在有了一个正在运行的EB服务器,该服务器使用ALB并处理端口9000上的websocket(ws)和UI(http)通信。

本文链接:https://www.f2er.com/3127154.html

大家都在问