重新命名了一些变量,增加了socket的namespace

This commit is contained in:
Xu Chang 2024-11-08 14:51:06 +08:00
parent 86c1eddf3e
commit 34b0a85ffd
2 changed files with 23 additions and 17 deletions

View File

@ -14,7 +14,8 @@ const types_1 = require("oak-domain/lib/types");
const cluster_adapter_1 = require("@socket.io/cluster-adapter");
const sticky_1 = require("@socket.io/sticky");
const socket_io_1 = require("socket.io");
const DATA_SUBSCRIBER_NAMESPACE = '/ds';
const DATA_SUBSCRIBE_NAMESPACE = '/dsn';
const SOCKET_NAMESPACE = '/sn';
const SERVER_SUBSCRIBER_NAMESPACE = process.env.OAK_SSUB_NAMESPACE || '/ssub';
const ExceptionMask = '内部不可知错误';
function concat(...paths) {
@ -39,7 +40,7 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
// socket
const httpServer = (0, http_1.createServer)(koa.callback());
const socketOption = {
path: connector.getSubscribeRouter(),
path: connector.getSocketPath(),
cors: process.env.NODE_ENV === 'development'
? {
origin: '*',
@ -66,8 +67,8 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
console.log('以单实例模式启动');
}
const appLoader = clusterInfo.usingCluster
? new oak_backend_base_1.ClusterAppLoader(path, io.of(DATA_SUBSCRIBER_NAMESPACE), io.of(SERVER_SUBSCRIBER_NAMESPACE), connector.getSubscribeRouter())
: new oak_backend_base_1.AppLoader(path, io.of(DATA_SUBSCRIBER_NAMESPACE));
? new oak_backend_base_1.ClusterAppLoader(path, io.of(DATA_SUBSCRIBE_NAMESPACE), io.of(SERVER_SUBSCRIBER_NAMESPACE), connector.getSocketPath())
: new oak_backend_base_1.AppLoader(path, io.of(DATA_SUBSCRIBE_NAMESPACE));
await appLoader.mount();
await appLoader.execStartRoutines();
if (routine) {
@ -164,7 +165,8 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
else {
url += `:${port}`;
}
url = concat(url, DATA_SUBSCRIBER_NAMESPACE);
const subscribeUrl = concat(url, DATA_SUBSCRIBE_NAMESPACE);
const socketUrl = concat(url, SOCKET_NAMESPACE);
// Example:
// import { io } from "socket.io-client";
// const socket = io('https://example.com/order', {
@ -173,11 +175,12 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
// the Socket instance is attached to the "order" Namespace
// the HTTP requests will look like: GET https://example.com/my-custom-path/?EIO=4&transport=polling&t=ML4jUwU
// 文档 https://socket.io/docs/v4/client-options/
router.get(connector.getSubscribePointRouter(), async (ctx) => {
router.get(connector.getSocketPointRouter(), async (ctx) => {
const { response } = ctx;
response.body = {
url,
path: (nginx?.socketPath ? `/${nginx.socketPath}` : '') + connector.getSubscribeRouter(),
socketUrl,
path: (nginx?.socketPath ? `/${nginx.socketPath}` : '') + connector.getSocketPath(),
subscribeUrl,
};
return;
});

View File

@ -16,7 +16,8 @@ import { setupWorker } from "@socket.io/sticky";
import { Server, ServerOptions } from "socket.io";
import { ServerConfiguration } from 'oak-domain/lib/types/Configuration';
const DATA_SUBSCRIBER_NAMESPACE = '/ds';
const DATA_SUBSCRIBE_NAMESPACE = '/dsn';
const SOCKET_NAMESPACE = '/sn';
const SERVER_SUBSCRIBER_NAMESPACE = process.env.OAK_SSUB_NAMESPACE || '/ssub';
const ExceptionMask = '内部不可知错误';
@ -57,7 +58,7 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
// socket
const httpServer = createServer(koa.callback());
const socketOption: Partial<ServerOptions> = {
path: connector.getSubscribeRouter(),
path: connector.getSocketPath(),
cors:
process.env.NODE_ENV === 'development'
? {
@ -89,11 +90,11 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
const appLoader = clusterInfo.usingCluster
? new ClusterAppLoader<ED, Cxt>(
path,
io.of(DATA_SUBSCRIBER_NAMESPACE),
io.of(DATA_SUBSCRIBE_NAMESPACE),
io.of(SERVER_SUBSCRIBER_NAMESPACE),
connector.getSubscribeRouter()
connector.getSocketPath()
)
: new AppLoader<ED, Cxt>(path, io.of(DATA_SUBSCRIBER_NAMESPACE));
: new AppLoader<ED, Cxt>(path, io.of(DATA_SUBSCRIBE_NAMESPACE));
await appLoader.mount();
await appLoader.execStartRoutines();
if (routine) {
@ -224,7 +225,8 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
} else {
url += `:${port}`;
}
url = concat(url, DATA_SUBSCRIBER_NAMESPACE);
const subscribeUrl = concat(url, DATA_SUBSCRIBE_NAMESPACE);
const socketUrl = concat(url, SOCKET_NAMESPACE);
// Example:
// import { io } from "socket.io-client";
// const socket = io('https://example.com/order', {
@ -233,11 +235,12 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
// the Socket instance is attached to the "order" Namespace
// the HTTP requests will look like: GET https://example.com/my-custom-path/?EIO=4&transport=polling&t=ML4jUwU
// 文档 https://socket.io/docs/v4/client-options/
router.get(connector.getSubscribePointRouter(), async (ctx) => {
router.get(connector.getSocketPointRouter(), async (ctx) => {
const { response } = ctx;
response.body = {
url,
path: (nginx?.socketPath ? `/${nginx.socketPath}`: '') + connector.getSubscribeRouter(),
socketUrl,
path: (nginx?.socketPath ? `/${nginx.socketPath}`: '') + connector.getSocketPath(),
subscribeUrl,
};
return;
});