WebSocketClient.js 586 Bytes
Newer Older
Alexander Bazo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import Observable from "../utils/Observable.js";
import { ConnectionOpenedEvent, DataEvent } from "./Events.js";

class WebSocketClient extends Observable {

  constructor() {
    super();
  }

  connect(url) {
    this.ws = new WebSocket(url);
    this.ws.onopen = onOpen.bind(this);
    this.ws.onmessage = onMessage.bind(this);
  }

}

function onOpen() {
  let connectionEvent = new ConnectionOpenedEvent();
  this.notifyAll(connectionEvent);
}

function onMessage(event) {
  let dataEvent = new DataEvent(event.data);
  this.notifyAll(dataEvent);
}

export default WebSocketClient;