Skip to content

Commit 9236447

Browse files
committed
feat: support collaborationWsPath config
1 parent 1480afa commit 9236447

4 files changed

Lines changed: 26 additions & 10 deletions

File tree

packages/collaboration/__tests__/browser/textmodel-binding.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ICodeEditor } from '@opensumi/ide-monaco';
99
import * as monaco from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api';
1010

1111
import { TextModelBinding } from '../../src/browser/textmodel-binding';
12-
import { ICollaborationService } from '../../src/common';
12+
import { ICollaborationService, COLLABORATION_PORT } from '../../src/common';
1313

1414
const injector = new Injector();
1515

@@ -40,7 +40,7 @@ describe('TextModelBinding test for yText and TextModel', () => {
4040

4141
beforeEach(() => {
4242
doc = new Y.Doc();
43-
wsProvider = new WebsocketProvider('ws://127.0.0.1:12345', 'test', doc, { connect: false }); // we don't use wsProvider here
43+
wsProvider = new WebsocketProvider(`ws://127.0.0.1:${COLLABORATION_PORT}`, 'test', doc, { connect: false }); // we don't use wsProvider here
4444
user1 = createBindingWithTextModel(doc, wsProvider.awareness);
4545
user2 = createBindingWithTextModel(doc, wsProvider.awareness);
4646
jest.mock('@opensumi/di');
@@ -205,7 +205,7 @@ describe('TextModelBinding test for editor', () => {
205205

206206
beforeAll(() => {
207207
doc = new Y.Doc();
208-
const wsProvider = new WebsocketProvider('ws://127.0.0.1:12345', 'test', doc, { connect: false });
208+
const wsProvider = new WebsocketProvider(`ws://127.0.0.1:${COLLABORATION_PORT}`, 'test', doc, { connect: false });
209209

210210
const {
211211
binding: _binding,

packages/collaboration/src/browser/collaboration.service.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,24 @@ export class CollaborationService extends WithEventBus implements ICollaboration
9595
}
9696

9797
initialize() {
98+
/**
99+
* 优先使用 appConfig.collaborationWsPath 配置
100+
* 如果没有该配置才根据 wsPath 去转换端口
101+
*/
102+
const { collaborationWsPath, wsPath } = this.appConfig;
103+
let serverUrl: string | undefined = collaborationWsPath;
104+
105+
if (!serverUrl) {
106+
const path = new URL(wsPath.toString());
107+
path.port = String(COLLABORATION_PORT);
108+
109+
serverUrl = path.toString();
110+
}
111+
98112
this.yDoc = new Y.Doc();
99113
this.yTextMap = this.yDoc.getMap();
100114

101-
// transform url
102-
const wsPath = new URL(this.appConfig.wsPath.toString());
103-
wsPath.port = String(COLLABORATION_PORT);
104-
this.yWebSocketProvider = new WebsocketProvider(wsPath.toString(), ROOM_NAME, this.yDoc);
115+
this.yWebSocketProvider = new WebsocketProvider(serverUrl.toString(), ROOM_NAME, this.yDoc);
105116

106117
this.yTextMap.observe(this.yMapObserver);
107118

packages/collaboration/src/node/y-websocket-server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { INodeLogger } from '@opensumi/ide-core-node';
99
import { FileChangeType, IFileService } from '@opensumi/ide-file-service';
1010
import { FileService } from '@opensumi/ide-file-service/lib/node';
1111

12-
import { IYWebsocketServer, ROOM_NAME } from '../common';
12+
import { COLLABORATION_PORT, IYWebsocketServer, ROOM_NAME } from '../common';
1313

1414
@Injectable()
1515
export class YWebsocketServerImpl implements IYWebsocketServer {
@@ -50,8 +50,8 @@ export class YWebsocketServerImpl implements IYWebsocketServer {
5050
this.websocketServer.handleUpgrade(req, socket, head, handleAuth);
5151
});
5252

53-
this.server.listen(12345, () => {
54-
this.logger.log('y-websocket server listening on port 12345');
53+
this.server.listen(COLLABORATION_PORT, () => {
54+
this.logger.log(`y-websocket server listening on port ${COLLABORATION_PORT}`);
5555
});
5656

5757
// init

packages/core-browser/src/react-providers/config-provider.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ export interface AppConfig {
232232
* 配置插件 browser 层的 component 样式文件和 iconfont 样式文件
233233
*/
234234
extensionBrowserStyleSheet?: ExtensionBrowserStyleSheet;
235+
/**
236+
* 定义协同模块的通信路径
237+
* 需要带端口号, 默认是 12345,可使用 COLLABORATION_PORT 字段来指定
238+
*/
239+
collaborationWsPath?: string;
235240
}
236241

237242
export const ConfigContext = React.createContext<AppConfig>({

0 commit comments

Comments
 (0)