闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
679 B

  1. import WebSocketClient from 'websocket.js';
  2. const createWebSocketURL = (url) => {
  3. const a = document.createElement('a');
  4. a.href = url;
  5. a.href = a.href;
  6. a.protocol = a.protocol.replace('http', 'ws');
  7. return a.href;
  8. };
  9. export default function getStream(streamingAPIBaseURL, accessToken, stream, { connected, received, disconnected, reconnected }) {
  10. const ws = new WebSocketClient(`${createWebSocketURL(streamingAPIBaseURL)}/api/v1/streaming/?access_token=${accessToken}&stream=${stream}`);
  11. ws.onopen = connected;
  12. ws.onmessage = e => received(JSON.parse(e.data));
  13. ws.onclose = disconnected;
  14. ws.onreconnect = reconnected;
  15. return ws;
  16. };