|
1 | 1 | import type net from 'net'; |
2 | 2 |
|
3 | | -import Fury, { Type } from '@furyjs/fury'; |
| 3 | +import { Type } from '@furyjs/fury'; |
4 | 4 | import type WebSocket from 'ws'; |
5 | 5 |
|
6 | 6 | import { EventEmitter } from '@opensumi/events'; |
7 | 7 | import { DisposableCollection } from '@opensumi/ide-core-common'; |
8 | 8 |
|
9 | 9 | import { NetSocketConnection, WSWebSocketConnection } from './connection'; |
10 | 10 | import { IConnectionShape } from './connection/types'; |
| 11 | +import { oneOf } from './fury-extends/one-of'; |
11 | 12 | import { createWebSocketConnection } from './message'; |
12 | 13 | import { Connection } from './rpc/connection'; |
13 | 14 | import { ILogger } from './types'; |
@@ -287,50 +288,56 @@ export class WSChannel implements IWebSocket { |
287 | 288 | } |
288 | 289 | } |
289 | 290 |
|
290 | | -export type MessageString = string & { |
291 | | - origin?: any; |
292 | | -}; |
293 | | - |
294 | | -/** |
295 | | - * 路径信息 ${pre}-${index} |
296 | | - */ |
297 | | -export class ChildConnectPath { |
298 | | - public pathPre = 'child_connect-'; |
299 | | - |
300 | | - getConnectPath(index: number, clientId: string) { |
301 | | - return `${this.pathPre}${index + 1}`; |
302 | | - } |
303 | | - |
304 | | - parseInfo(pathString: string) { |
305 | | - const list = pathString.split('-'); |
306 | | - |
307 | | - return { |
308 | | - pre: list[0], |
309 | | - index: list[1], |
310 | | - clientId: list[2], |
311 | | - }; |
312 | | - } |
313 | | -} |
| 291 | +export const PingProtocol = Type.object('ping', { |
| 292 | + clientId: Type.string(), |
| 293 | + id: Type.string(), |
| 294 | +}); |
314 | 295 |
|
315 | | -const fury = new Fury({}); |
| 296 | +export const PongProtocol = Type.object('pong', { |
| 297 | + clientId: Type.string(), |
| 298 | + id: Type.string(), |
| 299 | +}); |
316 | 300 |
|
317 | | -export const wsChannelProtocol = Type.object('ws-channel-protocol', { |
318 | | - kind: Type.string(), |
| 301 | +export const OpenProtocol = Type.object('open', { |
319 | 302 | clientId: Type.string(), |
320 | 303 | id: Type.string(), |
321 | 304 | path: Type.string(), |
| 305 | +}); |
| 306 | + |
| 307 | +export const ServerReadyProtocol = Type.object('server-ready', { |
| 308 | + id: Type.string(), |
| 309 | +}); |
| 310 | + |
| 311 | +export const DataProtocol = Type.object('data', { |
| 312 | + id: Type.string(), |
322 | 313 | content: Type.string(), |
| 314 | +}); |
| 315 | + |
| 316 | +export const BinaryProtocol = Type.object('binary', { |
| 317 | + id: Type.string(), |
323 | 318 | binary: Type.binary(), |
| 319 | +}); |
| 320 | + |
| 321 | +export const CloseProtocol = Type.object('close', { |
| 322 | + id: Type.string(), |
324 | 323 | code: Type.uint32(), |
325 | 324 | reason: Type.string(), |
326 | 325 | }); |
327 | 326 |
|
328 | | -const wsChannelProtocolSerializer = fury.registerSerializer(wsChannelProtocol); |
| 327 | +const serializer = oneOf([ |
| 328 | + PingProtocol, |
| 329 | + PongProtocol, |
| 330 | + OpenProtocol, |
| 331 | + ServerReadyProtocol, |
| 332 | + DataProtocol, |
| 333 | + BinaryProtocol, |
| 334 | + CloseProtocol, |
| 335 | +]); |
329 | 336 |
|
330 | 337 | export function stringify(obj: ChannelMessage): Uint8Array { |
331 | | - return wsChannelProtocolSerializer.serialize(obj); |
| 338 | + return serializer.serialize(obj); |
332 | 339 | } |
333 | 340 |
|
334 | 341 | export function parse(input: Uint8Array): ChannelMessage { |
335 | | - return wsChannelProtocolSerializer.deserialize(input) as any; |
| 342 | + return serializer.deserialize(input) as any; |
336 | 343 | } |
0 commit comments