#javascript #node.js #socket.io #digital-ocean #lance
#javascript #node.js #socket.io #digital-ocean #копье
Вопрос:
Моя игра lance-gg работает на локальном хостинге, но теперь, когда я попытался развернуть на digital ocean, сайт, похоже, размещен, но я не могу подключить socket.io . Ошибка в клиентском браузере:
[Error] Failed to load resource: The request timed out. (socket.io, line 0) http://144.126.196.39:3001/socket.io/?EIO=3amp;transport=pollingamp;t=NOU8Lc-
Код сервера:
import path from 'path';
import express from 'express';
import socketIO from 'socket.io';
import { Lib, ServerEngine, GameEngine } from 'lance-gg';
// define routes and socket
const server = express();
server.get('/', (req, res) => { res.sendFile(path.join(__dirname, '../dist/index.html')); });
server.use('/', express.static(path.join(__dirname, '../dist/')));
let requestHandler = server.listen(3001);
const io = socketIO(requestHandler);
// Game Instances
const gameEngine = new GameEngine({ traceLevel: Lib.Trace.TRACE_NONE });
const serverEngine = new ServerEngine(io, gameEngine, { debug: {}, updateRate: 12 });
serverEngine.start();
И код клиента:
import { Renderer, GameEngine, ClientEngine } from 'lance-gg';
const options = {
delayInputCount: 3,
scheduler: 'render-schedule',
syncOptions: {
sync: 'extrapolate',
remoteObjBending: 0.8,
bendingIncrements: 12
},
serverURL: 'http://144.126.196.39:3001'
};
// create a client engine and a game engine
const gameEngine = new GameEngine(options);
const clientEngine = new ClientEngine(gameEngine, options, Renderer);
document.addEventListener('DOMContentLoaded', (e) => clientEngine.start());
Я понимаю, что шаблон NodeJS starter в digital ocean использует Nginx, поэтому я обязательно перенаправил его с помощью порта 3001 в свое приложение.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/moon-game-2;
index index.html index.htm index.nginx-debian.html;
server_name hellonode;
location ^~ /assets/ {
gzip_static on;
expires 12h;
add_header Cache-Control public;
}
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:3001;
}
}
Как я могу сделать так, чтобы моя розетка.ввод-вывод также подключается к моему серверу lance-gg?
Ответ №1:
Неважно, решил мою проблему, которая была в клиентском коде. Очевидно, что в облачной среде мне не нужно указывать порт 3001, как я делаю на localhost.
// ...
import { USE_CLOUD_SERVER } from './constants';
const options = {
// ...
// The cloud server is set up with Nginx to redirect to the right port.
serverURL: USE_CLOUD_SERVER ? 'http://144.126.196.39' : 'http://localhost:3001'
};
// ...