Skip to content

Commit 57fa22c

Browse files
shenjackyuanjiemoonrailgun
authored andcommitted
chore: Add socket msgpack related configs and env
1 parent 3801bb2 commit 57fa22c

File tree

8 files changed

+57
-17
lines changed

8 files changed

+57
-17
lines changed

client/packages/tailchat-client-sdk/src/openapi/client/ws.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,39 @@ import type { ChatMessage } from 'tailchat-types';
66
export class TailchatWsClient extends TailchatBaseClient {
77
public socket: Socket | null = null;
88

9+
constructor(
10+
public url: string,
11+
public appId: string,
12+
public appSecret: string,
13+
public useMsgpack: boolean = true
14+
) {
15+
super(url, appId, appSecret);
16+
}
17+
918
connect(): Promise<Socket> {
1019
return new Promise<Socket>(async (resolve, reject) => {
1120
await this.waitingForLogin();
1221

1322
const token = this.jwt;
14-
15-
const socket = (this.socket = io(this.url, {
16-
transports: ['websocket'],
17-
auth: {
18-
token,
19-
},
20-
forceNew: true,
21-
parser: msgpackParser,
22-
}));
23+
let socket: Socket;
24+
if (this.useMsgpack) {
25+
socket = this.socket = io(this.url, {
26+
transports: ['websocket'],
27+
auth: {
28+
token,
29+
},
30+
forceNew: true,
31+
parser: msgpackParser,
32+
});
33+
} else {
34+
socket = this.socket = io(this.url, {
35+
transports: ['websocket'],
36+
auth: {
37+
token,
38+
},
39+
forceNew: true,
40+
});
41+
}
2342

2443
socket.once('connect', () => {
2544
// 连接成功

client/shared/api/socket.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { showErrorToasts, showGlobalLoading, showToasts } from '../manager/ui';
66
import { t } from '../i18n';
77
import { sharedEvent } from '../event';
88
import msgpackParser from 'socket.io-msgpack-parser';
9+
import { getGlobalConfig } from '../model/config';
910

1011
class SocketEventError extends Error {
1112
name = 'SocketEventError';
@@ -207,14 +208,24 @@ export function createSocket(token: string): Promise<AppSocket> {
207208
}
208209

209210
return new Promise((resolve, reject) => {
210-
_socket = io(getServiceUrl(), {
211-
transports: ['websocket'],
212-
auth: {
213-
token,
214-
},
215-
forceNew: true,
216-
parser: msgpackParser,
217-
});
211+
if (getGlobalConfig().disableSocketMsgpack) {
212+
_socket = io(getServiceUrl(), {
213+
transports: ['websocket'],
214+
auth: {
215+
token,
216+
},
217+
forceNew: true,
218+
});
219+
} else {
220+
_socket = io(getServiceUrl(), {
221+
transports: ['websocket'],
222+
auth: {
223+
token,
224+
},
225+
forceNew: true,
226+
parser: msgpackParser,
227+
});
228+
}
218229
_socket.once('connect', () => {
219230
// 连接成功
220231
const appSocket = new AppSocket(_socket);

client/shared/model/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export interface GlobalConfig {
3434
*/
3535
serverEntryImage?: string;
3636

37+
/**
38+
* 是否禁用 Socketio 的 Msgpack 解析器
39+
*/
40+
disableSocketMsgpack?: boolean;
41+
3742
/**
3843
* 是否禁用注册功能
3944
*/

client/shared/utils/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const defaultGlobalConfig: GlobalConfig = {
2323
uploadFileLimit: 1 * 1024 * 1024,
2424
emailVerification: false,
2525
serverName: 'Tailchat',
26+
disableSocketMsgpack: false,
2627
disableUserRegister: false,
2728
disableGuestLogin: false,
2829
disableCreateGroup: false,

server/packages/sdk/src/services/lib/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const config = {
6363
websiteId: process.env.TIANJI_WEBSITE_ID,
6464
},
6565
feature: {
66+
disableSocketMsgpack: checkEnvTrusty(process.env.DISABLE_SOCKET_MSGPACK), // 是否禁用socketio的 msgpack parser
6667
disableFileCheck: checkEnvTrusty(process.env.DISABLE_FILE_CHECK),
6768
disableLogger: checkEnvTrusty(process.env.DISABLE_LOGGER), // 是否关闭日志
6869
disableUserRegister: checkEnvTrusty(process.env.DISABLE_USER_REGISTER), // 是否关闭用户注册功能

server/services/core/config.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class ConfigService extends TcService {
8080
tianji: config.tianji,
8181
uploadFileLimit: config.storage.limit,
8282
emailVerification: config.emailVerification,
83+
disableSocketMsgpack: config.feature.disableSocketMsgpack,
8384
disableUserRegister: config.feature.disableUserRegister,
8485
disableGuestLogin: config.feature.disableGuestLogin,
8586
disableCreateGroup: config.feature.disableCreateGroup,

website/docs/deployment/environment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ title: Environment Variable
2727
| REQUEST_TIMEOUT | 10000 | Number of milliseconds to wait before reject a request with a RequestTimeout error. Disabled: 0 |
2828
| 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`) |
2929
| TIANJI_WEBSITE_ID | - | Tianji website id |
30+
| DISABLE_SOCKET_MSGPACK | - | Whether to disable socket using messagepack, if "1" or "true" turn off this method |
3031
| DISABLE_LOGGER | - | Whether to disable the log output, if "1" or "true" turn off the log on the fly |
3132
| DISABLE_USER_REGISTER | - | Whether to disable the user register, if "1" or "true" turn off this method |
3233
| DISABLE_GUEST_LOGIN | - | Whether to disable the guest login, if "1" or "true" turn off this method |

website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/deployment/environment.md

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

0 commit comments

Comments
 (0)