|
1 | | -import { parse } from '@deepstream/protobuf/dist/src/message-parser' |
2 | | -import { getMessage } from '@deepstream/protobuf/dist/src/message-builder' |
3 | | -import {Socket} from '../deepstream-client' |
4 | | -import { JSONObject, TOPIC, Message, CONNECTION_ACTION } from '../constants' |
| 1 | +import { JSONObject } from '../constants' |
| 2 | +import { wireSocket, hasMeaningfulSocketOptions, SocketFactory } from './socket-factory-shared' |
5 | 3 |
|
6 | | -const BrowserWebsocket = (global.WebSocket || global.MozWebSocket) as any |
| 4 | +export { SocketFactory } from './socket-factory-shared' |
7 | 5 |
|
8 | | -export type SocketFactory = (url: string, options: JSONObject, heartBeatInterval: number) => Socket |
| 6 | +const NativeWS = (global as any).WebSocket as any |
9 | 7 |
|
10 | | -export const socketFactory: SocketFactory = (url, options = { jsonTransportMode: false }, heartBeatInterval) => { |
11 | | - const socket = BrowserWebsocket |
12 | | - ? new BrowserWebsocket(url, [], options) |
13 | | - : new (require('ws'))(url, options) as any |
14 | | - |
15 | | - if (BrowserWebsocket && options.jsonTransportMode !== true) { |
16 | | - socket.binaryType = 'arraybuffer' |
17 | | - } |
18 | | - |
19 | | - const buildMessage = options.jsonTransportMode !== true ? getMessage : (message: Message, isAck: boolean) => JSON.stringify({ ...message, isAck }) |
20 | | - |
21 | | - const pingMessage = buildMessage({ topic: TOPIC.CONNECTION, action: CONNECTION_ACTION.PING }, false) |
22 | | - let pingInterval: number | null = null |
23 | | - let lastRecievedMessageTimestamp = -1 |
24 | | - |
25 | | - // tslint:disable-next-line:no-empty |
26 | | - socket.onparsedmessage = () => {} |
27 | | - socket.onmessage = (raw: {data: Buffer | string }) => { |
28 | | - lastRecievedMessageTimestamp = Date.now() |
29 | | - let parseResults |
30 | | - if (options.jsonTransportMode !== true) { |
31 | | - parseResults = parse(BrowserWebsocket ? new Buffer(new Uint8Array(raw.data as Buffer)) : raw.data as Buffer) |
32 | | - } else { |
33 | | - parseResults = [JSON.parse(raw.data as string)] |
34 | | - } |
35 | | - socket.onparsedmessages(parseResults) |
36 | | - } |
37 | | - socket.getTimeSinceLastMessage = () => { |
38 | | - if (lastRecievedMessageTimestamp < 0) return 0 |
39 | | - return Date.now() - lastRecievedMessageTimestamp |
| 8 | +export const socketFactory: SocketFactory = (url: string, options: JSONObject = { jsonTransportMode: false }, heartBeatInterval: number) => { |
| 9 | + if (NativeWS && !hasMeaningfulSocketOptions(options)) { |
| 10 | + return wireSocket(new NativeWS(url, []), options, heartBeatInterval, true) |
40 | 11 | } |
41 | | - socket.sendParsedMessage = (message: Message): void => { |
42 | | - if (message.topic === TOPIC.CONNECTION && message.action === CONNECTION_ACTION.CLOSING) { |
43 | | - socket.onparsedmessages([{ topic: TOPIC.CONNECTION, action: CONNECTION_ACTION.CLOSED }]) |
44 | | - socket.close() |
45 | | - return |
46 | | - } |
47 | | - if (message.parsedData) { |
48 | | - message.data = JSON.stringify(message.parsedData) |
49 | | - } |
50 | | - // if (message.action !== CONNECTION_ACTIONS.PONG && message.action !== CONNECTION_ACTIONS.PING) { |
51 | | - // console.log('>>>', TOPIC[message.topic], (ACTIONS as any)[message.topic][message.action], message.parsedData, message.data, message.name) |
52 | | - // } |
53 | | - if (message.data === undefined) { |
54 | | - delete message.data |
55 | | - } |
56 | | - socket.send(buildMessage(message, false)) |
57 | | - } |
58 | | - |
59 | | - socket.onclosed = null |
60 | | - socket.onclose = () => { |
61 | | - clearInterval(pingInterval!) |
62 | | - socket.onclosed() |
63 | | - } |
64 | | - |
65 | | - socket.onopened = null |
66 | | - socket.onopen = () => { |
67 | | - pingInterval = setInterval(() => { |
68 | | - try { |
69 | | - socket.send(pingMessage) |
70 | | - } catch (e) { |
71 | | - clearTimeout(pingInterval!) |
72 | | - } |
73 | | - }, heartBeatInterval) as never as number |
74 | | - socket.onopened() |
75 | | - } |
76 | | - |
77 | | - return socket |
| 12 | + const WS = require('ws') |
| 13 | + return wireSocket(new WS(url, options), options, heartBeatInterval, false) |
78 | 14 | } |
0 commit comments