This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2017-02-03 15:34:31 -08:00
|
|
|
import WebSocketClient from 'websocket.js';
|
|
|
|
|
|
|
|
const createWebSocketURL = (url) => {
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
|
|
|
a.href = url;
|
|
|
|
a.href = a.href;
|
|
|
|
a.protocol = a.protocol.replace('http', 'ws');
|
|
|
|
|
|
|
|
return a.href;
|
|
|
|
};
|
|
|
|
|
2017-02-07 05:37:12 -08:00
|
|
|
export default function getStream(accessToken, stream, { connected, received, disconnected, reconnected }) {
|
2017-02-03 15:34:31 -08:00
|
|
|
const ws = new WebSocketClient(`${createWebSocketURL(STREAMING_API_BASE_URL)}/api/v1/streaming/?access_token=${accessToken}&stream=${stream}`);
|
|
|
|
|
2017-02-07 05:37:12 -08:00
|
|
|
ws.onopen = connected;
|
|
|
|
ws.onmessage = e => received(JSON.parse(e.data));
|
|
|
|
ws.onclose = disconnected;
|
|
|
|
ws.onreconnect = reconnected;
|
2017-02-03 15:34:31 -08:00
|
|
|
|
|
|
|
return ws;
|
|
|
|
};
|