socket.js 591 B

12345678910111213141516171819202122232425
  1. (function (App) {
  2. // reconnect if model or socket is reset
  3. App.socket.events.once('changed', _.socket);
  4. App.model.events.once('changed', _.socket);
  5. // connect to server
  6. var keepAlive
  7. , socket = App.socket()
  8. , model = App.model();
  9. socket.onclose = function () {
  10. if (keepAlive) keepAlive = window.clearInterval(keepAlive)
  11. model.server.set(false) };
  12. socket.onopen = function () {
  13. keepAlive = window.setInterval(function () { socket.send("ping"); }, 30000)
  14. model.put('server', true) };
  15. model.put('connection', require('q-connection')(socket));
  16. })