Websocket wrapper

Websocket wrapper class with WebSocket.IN api

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/408004/935287/Websocket%20wrapper.js

  1. /*
  2. Websocket wrapper class with WebSocket.IN api
  3. */
  4.  
  5. class WebSocketIn {
  6. constructor(channelId) {
  7. this.channelId = channelId;
  8. this.ws = null;
  9. this.connect();
  10. }
  11.  
  12. connect() {
  13. const APIKEY = 'QipkWZUSG1D0KxYxHs3lp8vm6iTwa4Dv7xgG9PHpbQ56QmqKdaQVhi4DK1A8';
  14. const url = `ws:/172.16.0.217:1880/v3/${this.channelId}?apiKey=${APIKEY}`;
  15. this.ws = new WebSocket(url);
  16. console.log(`Connecting to: ${url}`);
  17.  
  18. this.ws.onmessage = (event) => {
  19. this.onmessage(event);
  20. };
  21.  
  22. this.ws.onopen = () => {
  23. console.log('Websocket connected!');
  24. };
  25.  
  26. this.ws.onerror = (err) => {
  27. console.log(`Websocket error: ${err.message}`);
  28. this.ws.close();
  29. };
  30.  
  31. this.ws.onclose = () => {
  32. console.log('Websocket closed!');
  33. setTimeout(() => {
  34. this.connect();
  35. }, 2500);
  36. };
  37. }
  38.  
  39. onmessage(event) {
  40. console.log('parent');
  41. console.log(event.data);
  42. }
  43.  
  44. send(data) {
  45. this.ws.send(data);
  46. }
  47. }