connect.js 687 B

123456789101112131415161718192021222324
  1. (function () {
  2. // connect to server
  3. var keepAlive
  4. , socket = new WebSocket("ws://" + window.location.host + window.location.pathname);
  5. socket.onclose = function () {
  6. if (keepAlive) keepAlive = window.clearInterval(keepAlive)
  7. $.state.server.set(false)
  8. };
  9. socket.onopen = function () {
  10. keepAlive = window.setInterval(function () { socket.send("ping"); }, 30000)
  11. $.state.server.set(true)
  12. };
  13. $.state.connection.set(require('q-connection')(socket));
  14. // subscribe to server-side persistence store
  15. var update = $.emit("update");
  16. $.lib.q.done($.api("subscribe", update), update,
  17. $.lib.error("could not subscribe to server"));
  18. return socket;
  19. })