好的,我知道了。这就是我要使所有端口9000都能正常工作的方法。
在EB中,创建一个应用程序,然后开始创建环境。
在环境配置中,进入软件部分,并告诉您您将使用npm run prod
来启动服务器。
现在,进入负载均衡器部分,如下图所示:
- 在端口9000上添加侦听器
- 在端口9000上创建一个进程并启用粘性(我称为mine peerjsServer)
- 为要用于访问服务器的每个URL路径添加规则,并将每个规则分配给您创建的进程(peerjsServer)。另外,将默认值指向该过程,以便对80进行运行状况检查成功到达您的服务器
您可能需要跳到AWS UI中的 EC2仪表板,以确保在那里定义了必要的安全组。我想我在上创建了前两个,最后两个是默认创建,但我不记得了。无论如何,他们需要为入站和出站打开端口9000(默认情况下始终始终存在端口80):
返回 EB配置,转到实例部分,并确保为您的实例分配了安全组:
我运行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