我对此感到非常愚蠢,但这是我的问题。
我在同一NodeTS App中使用Socket-Controller和routing-controller。因此,我需要创建两个快速服务器。一种正在监听端口3000或任何环境,另一种正在监听端口65080。在本地,这非常理想。现在在GAE上,正常请求的HTTP Server可以正常工作,而SocketServer总是超时。
如果有帮助,这是我的app.yaml:
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"
好吧,解决方案相当简单。
我需要将Socket Server绑定到Express正在使用的同一HTTP Server实例。以下是我的更改。
socketLoader.ts
const io = require('socket.io')(settings.getData('express_server'));
useSocketServer(io, {
controllers: env.app.dirs.controllers,
middlewares: env.app.dirs.middlewares,
});
其中express_server是expressApp.listen(env.app.port)返回的服务器实例。
我可以问一下您何时在app.js的加载器数组中调用sovketLoader吗?我的问题是服务器启动时没有错误,但是我无法连接到套接字。我认为这是因为我必须在expressApp.listen()之前调用useSocketServer。编辑:我正在使用相同的样板
弄清楚了!如果其他人有相同的问题:请使用socket.io版本2.X或启用allowEIO3:true,在版本3.X中,并在app.ts中的expressLoader之后调用socketLoader