chore: Add socket msgpack related configs and env

pull/249/head
shenjack 11 months ago committed by moonrailgun
parent 3801bb20a8
commit 57fa22cd8d

@ -6,20 +6,39 @@ import type { ChatMessage } from 'tailchat-types';
export class TailchatWsClient extends TailchatBaseClient {
public socket: Socket | null = null;
constructor(
public url: string,
public appId: string,
public appSecret: string,
public useMsgpack: boolean = true
) {
super(url, appId, appSecret);
}
connect(): Promise<Socket> {
return new Promise<Socket>(async (resolve, reject) => {
await this.waitingForLogin();
const token = this.jwt;
const socket = (this.socket = io(this.url, {
let socket: Socket;
if (this.useMsgpack) {
socket = this.socket = io(this.url, {
transports: ['websocket'],
auth: {
token,
},
forceNew: true,
parser: msgpackParser,
}));
});
} else {
socket = this.socket = io(this.url, {
transports: ['websocket'],
auth: {
token,
},
forceNew: true,
});
}
socket.once('connect', () => {
// 连接成功

@ -6,6 +6,7 @@ import { showErrorToasts, showGlobalLoading, showToasts } from '../manager/ui';
import { t } from '../i18n';
import { sharedEvent } from '../event';
import msgpackParser from 'socket.io-msgpack-parser';
import { getGlobalConfig } from '../model/config';
class SocketEventError extends Error {
name = 'SocketEventError';
@ -207,6 +208,15 @@ export function createSocket(token: string): Promise<AppSocket> {
}
return new Promise((resolve, reject) => {
if (getGlobalConfig().disableSocketMsgpack) {
_socket = io(getServiceUrl(), {
transports: ['websocket'],
auth: {
token,
},
forceNew: true,
});
} else {
_socket = io(getServiceUrl(), {
transports: ['websocket'],
auth: {
@ -215,6 +225,7 @@ export function createSocket(token: string): Promise<AppSocket> {
forceNew: true,
parser: msgpackParser,
});
}
_socket.once('connect', () => {
// 连接成功
const appSocket = new AppSocket(_socket);

@ -34,6 +34,11 @@ export interface GlobalConfig {
*/
serverEntryImage?: string;
/**
* Socketio Msgpack
*/
disableSocketMsgpack?: boolean;
/**
*
*/

@ -23,6 +23,7 @@ export const defaultGlobalConfig: GlobalConfig = {
uploadFileLimit: 1 * 1024 * 1024,
emailVerification: false,
serverName: 'Tailchat',
disableSocketMsgpack: false,
disableUserRegister: false,
disableGuestLogin: false,
disableCreateGroup: false,

@ -63,6 +63,7 @@ export const config = {
websiteId: process.env.TIANJI_WEBSITE_ID,
},
feature: {
disableSocketMsgpack: checkEnvTrusty(process.env.DISABLE_SOCKET_MSGPACK), // 是否禁用socketio的 msgpack parser
disableFileCheck: checkEnvTrusty(process.env.DISABLE_FILE_CHECK),
disableLogger: checkEnvTrusty(process.env.DISABLE_LOGGER), // 是否关闭日志
disableUserRegister: checkEnvTrusty(process.env.DISABLE_USER_REGISTER), // 是否关闭用户注册功能

@ -80,6 +80,7 @@ class ConfigService extends TcService {
tianji: config.tianji,
uploadFileLimit: config.storage.limit,
emailVerification: config.emailVerification,
disableSocketMsgpack: config.feature.disableSocketMsgpack,
disableUserRegister: config.feature.disableUserRegister,
disableGuestLogin: config.feature.disableGuestLogin,
disableCreateGroup: config.feature.disableCreateGroup,

@ -27,6 +27,7 @@ title: Environment Variable
| REQUEST_TIMEOUT | 10000 | Number of milliseconds to wait before reject a request with a RequestTimeout error. Disabled: 0 |
| TIANJI_SCRIPT_URL | - | Script Url of Tianji if you wanna monitor Tailchat user usage, you can get it in code modal in Tianji website (example: `https://tianji.example.com/tracker.js`) |
| TIANJI_WEBSITE_ID | - | Tianji website id |
| DISABLE_SOCKET_MSGPACK | - | Whether to disable socket using messagepack, if "1" or "true" turn off this method |
| DISABLE_LOGGER | - | Whether to disable the log output, if "1" or "true" turn off the log on the fly |
| DISABLE_USER_REGISTER | - | Whether to disable the user register, if "1" or "true" turn off this method |
| DISABLE_GUEST_LOGIN | - | Whether to disable the guest login, if "1" or "true" turn off this method |

@ -27,6 +27,7 @@ title: 环境变量
| REQUEST_TIMEOUT | 10000 | 请求超时毫秒数,请求超过该时间没有完成会抛出 `RequestTimeout` 错误。 如果需要禁用请求超时限制传0 |
| TIANJI_SCRIPT_URL | - | Tianji 脚本 URL如需监控 Tailchat 用户使用情况,可在天际网站代码模式中获取 (例如:`https://tianji.example.com/tracker.js`) |
| TIANJI_WEBSITE_ID | - | Tianji 网站 id |
| DISABLE_SOCKET_MSGPACK | - | 是否禁用socket使用messagepack, 如果为 "1" 或者 "true" 则禁用该功能 |
| DISABLE_LOGGER | - | 是否禁用日志输出, 如果为 "1" 或者 "true" 则在运行中关闭日志 |
| DISABLE_USER_REGISTER | - | 是否关闭用户注册功能, 如果为 "1" 或者 "true" 则关闭该功能 |
| DISABLE_GUEST_LOGIN | - | 是否关闭用户游客登录功能, 如果为 "1" 或者 "true" 则关闭该功能 |

Loading…
Cancel
Save