2017-02-04 00:34:31 +01: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-04-15 02:32:42 +02:00
|
|
|
export default function getStream(streamingAPIBaseURL, accessToken, stream, { connected, received, disconnected, reconnected }) {
|
|
|
|
const ws = new WebSocketClient(`${createWebSocketURL(streamingAPIBaseURL)}/api/v1/streaming/?access_token=${accessToken}&stream=${stream}`);
|
2017-02-04 00:34:31 +01:00
|
|
|
|
2017-02-07 14:37:12 +01:00
|
|
|
ws.onopen = connected;
|
|
|
|
ws.onmessage = e => received(JSON.parse(e.data));
|
|
|
|
ws.onclose = disconnected;
|
|
|
|
ws.onreconnect = reconnected;
|
2017-02-04 00:34:31 +01:00
|
|
|
|
|
|
|
return ws;
|
|
|
|
};
|