Node.js Google应用程序引擎(Flex)使用express服务器和单独的socket.io服务器
我觉得自己很愚蠢,但这是我的问题 我在同一个节点应用程序中使用套接字控制器和路由控制器。因此,我需要创建两个express服务器。一个在监听端口3000或其他env,另一个在端口65080。在本地,这是完美的。现在在GAE上,我的HTTP服务器正常工作,而我的SocketServer总是超时 如果有帮助,以下是我的app.yaml:Node.js Google应用程序引擎(Flex)使用express服务器和单独的socket.io服务器,node.js,google-app-engine,Node.js,Google App Engine,我觉得自己很愚蠢,但这是我的问题 我在同一个节点应用程序中使用套接字控制器和路由控制器。因此,我需要创建两个express服务器。一个在监听端口3000或其他env,另一个在端口65080。在本地,这是完美的。现在在GAE上,我的HTTP服务器正常工作,而我的SocketServer总是超时 如果有帮助,以下是我的app.yaml: runtime: nodejs env: flex # This sample incurs costs to run on the App Engine fle
runtime: nodejs
env: flex
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: .5
memory_gb: 0.5
disk_size_gb: 10
network:
session_affinity: true
forwarded_ports:
- 65080
env_variables:
APP_NAME: Backend
APP_SCHEMA: https
APP_HOST: localhost
APP_PORT: 3000
APP_ROUTE_PREFIX: /api
APP_BANNER: true
TYPEORM_CONNECTION: postgres
TYPEORM_USERNAME: postgres
TYPEORM_DATABASE: db
TYPEORM_SYNCHRONIZE: true
TYPEORM_LOGGING: error
TYPEORM_LOGGER: advanced-console
我正在用它创建SocketServer:
import {MicroframeworkLoader, MicroframeworkSettings} from 'microframework-w3tec';
import {createSocketServer} from 'socket-controllers';
import {env} from '../env';
export const socketLoader: MicroframeworkLoader = (settings: MicroframeworkSettings | undefined) => {
if (!settings) {
return;
}
const io = createSocketServer(65080, {
controllers: env.app.dirs.controllers,
middlewares: env.app.dirs.middlewares,
});
settings.setData('socket_io', io);
};
顺便说一下,我用的是样板
感谢您的帮助,因为这让我很沮丧。:)
编辑:创建第二个微服务实际上不是一个选项,因为它们共享的内容太多,无法彼此隔离。(服务、数据库模型、设置)
编辑:我使用以下配置防火墙:
--allow tcp:65080 \
--target-tags websocket \
--description "Allow websocket traffic on port 65080"
嗯,解决办法相当简单 我需要将套接字服务器绑定到express使用的同一HTTP服务器实例。以下是我的变化 socketLoader.ts
const io = require('socket.io')(settings.getData('express_server'));
useSocketServer(io, {
controllers: env.app.dirs.controllers,
middlewares: env.app.dirs.middlewares,
});
其中express\u server是expressApp.listen(env.app.port)返回的服务器实例。解决方案相当简单 我需要将套接字服务器绑定到express使用的同一HTTP服务器实例。以下是我的变化 socketLoader.ts
const io = require('socket.io')(settings.getData('express_server'));
useSocketServer(io, {
controllers: env.app.dirs.controllers,
middlewares: env.app.dirs.middlewares,
});
其中express\u server是expressApp.listen(env.app.port)返回的服务器实例。请问您什么时候在app.js的loaders数组中调用sovketLoader?我的问题是服务器启动时没有错误,但无法连接到套接字。我想这是因为我必须在expressApp.listen()之前调用useSocketServer。编辑:我用的是同一个样板,我想出来了!如果其他人也有同样的问题:在版本3.X中使用socket.io版本2.X或启用allowio3:true,并在app.ts中的expressLoader之后调用socketLoader。请问您何时在app.js中的loaders数组中调用sovketLoader?我的问题是服务器启动时没有错误,但无法连接到套接字。我想这是因为我必须在expressApp.listen()之前调用useSocketServer。编辑:我用的是同一个样板,我想出来了!如果其他人也有同样的问题:在版本3.X中使用socket.io版本2.X或启用allowio3:true,并在app.ts中的expressLoader之后调用socketLoader