ws#Data TypeScript Examples

The following examples show how to use ws#Data. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VoiceGateway.ts    From evolvejs with GNU Affero General Public License v3.0 6 votes vote down vote up
public init(): void {
		let endpoint = this.gateway.voiceServerUpdate.d.endpoint;
		if (!endpoint) return;
		endpoint = endpoint.match(/([^:]*)/)[0];
		this.websocket = new ws(`wss://${endpoint}?v=4`);

		this.websocket.on("open", () => {
			VoiceIdentify.d.server_id = this.gateway.voiceServerUpdate.d.server_id;
			VoiceIdentify.d.user_id = this.gateway.voiceStateUpdate.userId;
			VoiceIdentify.d.session_id = this.gateway.voiceStateUpdate.sessionID;
			VoiceIdentify.d.token = this.gateway.voiceServerUpdate.d.token;
			this.websocket.send(JSON.stringify(VoiceIdentify));
		});

		this.websocket.on("error", (e) =>
			this.gateway.ws.manager.builder.client.transformer.error(e.message)
		);

		this.websocket.on("message", (data: Data) => {
			this.handle(data);
		});
	}
Example #2
Source File: VoiceGateway.ts    From evolvejs with GNU Affero General Public License v3.0 6 votes vote down vote up
public handle(data: Data): void {
		const payload: Payload = JSON.parse(data.toString());
		const { op, d } = payload;
		if (op == 2) {
			this.emit("ready" as EVENTS, payload);
		} else if (op == 8) {
			setInterval(() => {
				Heartbeat.op = 3;
				if (this.seq) Heartbeat.d = this.seq;
				this.websocket.send(JSON.stringify(Heartbeat));
			}, d.heartbeat_interval);
		} else if (op == 6) {
			this.seq = d;
		}
	}
Example #3
Source File: data.ts    From bee-js with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
export async function prepareWebsocketData(data: Data | BlobPolyfill): Promise<Uint8Array> | never {
  if (typeof data === 'string') return new TextEncoder().encode(data)

  if (data instanceof Buffer) return new Uint8Array(data)

  if (data instanceof ArrayBuffer) return new Uint8Array(data)

  if (isBufferArray(data)) return new Uint8Array(Buffer.concat(data))

  throw new TypeError('unknown websocket data type')
}
Example #4
Source File: Connection.ts    From skychat with MIT License 6 votes vote down vote up
/**
     * When a message is received on the socket
     * @param data
     */
    private async onMessage(data: Data): Promise<void> {

        try {

            // If data is not of type string, fail with error
            if (data instanceof Buffer) {
                this.emit('audio', data);
                return;
            }

            // Otherwise, if type is not string, reject the message
            if (typeof data !== 'string') {
                throw new Error('Unsupported data type');
            }

            // Decode & unpack message
            const decodedMessage = JSON.parse(data);
            const eventName = decodedMessage.event;
            const payload = decodedMessage.data;

            // Check that the event name is valid and is registered
            if (typeof eventName !== 'string') {
                throw new Error('Event could not be parsed');
            }

            // Call handler
            this.emit(eventName, payload);

        } catch (error) {

            this.sendError(error);
        }
    }