簡體   English   中英

Lance-gg 游戲 socket.io 無法在數字海洋上加載資源

[英]Lance-gg game socket.io failed to load resource on digital ocean

我的 lance-gg 游戲可以在 localhost 上運行,但是現在我已經嘗試在數字海洋上進行部署,該站點似乎正在托管,但我無法連接 socket.io。 客戶端瀏覽器中的錯誤是:

[Error] Failed to load resource: The request timed out. (socket.io, line 0) http://144.126.196.39:3001/socket.io/?EIO=3&transport=polling&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 入門模板使用 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;
        }

}

我怎樣才能讓我的 socket.io 也連接到我的 lance-gg 服務器?

沒關系,解決了我在客戶端代碼中的問題。 顯然,在雲環境中,我不需要像在 localhost 上那樣指定端口 3001。

// ...

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'
};

// ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM