首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lance-gg游戏socket.io未能在数字海洋上装载资源。

Lance-gg游戏socket.io未能在数字海洋上装载资源。
EN

Stack Overflow用户
提问于 2020-12-01 12:06:26
回答 1查看 116关注 0票数 1

我的兰斯-g游戏在本地主机上工作,但是现在我已经尝试在数字海洋上部署,这个网站似乎是托管的,但我无法连接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-

服务器代码是:

代码语言:javascript
复制
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();

客户端代码是:

代码语言:javascript
复制
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重定向到我的应用程序。

代码语言:javascript
复制
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也连接到我的兰斯-gg服务器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-01 12:25:08

没想过,解决了我在客户代码中的问题。显然,在云环境中,我不需要像在localhost上那样指定端口3001。

代码语言:javascript
复制
// ...

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

// ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65090174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档