Advertisement
Guest User

Websocket

a guest
Aug 29th, 2014
7,393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var WebsocketClass = function(host){
  2.                this.socket = new WebSocket(host);
  3.         }
  4.  
  5.         var current_interval_id;
  6.         var reconnecting_attempt = false;
  7.         WebsocketClass.prototype = {
  8.                initWebsocket : function(){
  9.                        var $this = this;
  10.                        this.socket.onmessage = function(e){
  11.                                $this._onMessageEvent(e);
  12.                        };
  13.                        this.socket.onclose = function(){
  14.                                $this._onCloseEvent();
  15.                        };
  16.                        this.socket.onopen = function() {
  17.                                $this._onOpenEvent();
  18.                        }
  19.                        this.socket.onerror = function() {
  20.                                $this._onCloseEvent();
  21.                        }
  22.                },
  23.                initAutoSend : function(){
  24.                     var $this = this;
  25.                     current_interval_id = setInterval(function(){$this.socket.send('update')}, {{ settings.updatetime }})
  26.                },
  27.                _onOpenEvent : function() {
  28.                     Messenger().post({
  29.                       message: 'Connected to Server.',
  30.                       type: 'success'
  31.                     });
  32.                },
  33.                _onMessageEvent : function(e){
  34.                     var parsed = JSON.parse(e.data);
  35.                     if (parsed['received'] == 'update') {
  36.                         $("#cpuload").text(parsed['cpu']+" %");
  37.                         $("#ram").text(parsed['ram']+" %");
  38.                         $("#disk").text(parsed['disk']+" %");
  39.                     }
  40.                     else {
  41.                         Messenger().post({
  42.                           message: parsed['received'],
  43.                           type: 'success'
  44.                         });
  45.                     }
  46.                },
  47.                _onCloseEvent : function(){
  48.                     window.clearInterval(current_interval_id)
  49.                     if(reconnecting_attempt == false) {
  50.                         reconnecting_attempt = true;
  51.                         var connect = 0;
  52.                         window.clearInterval(current_interval_id);
  53.                         $('#cpuload').text('');
  54.                         $('#cpuload').prepend("<img src='/static/img/loading.gif' />")
  55.                         $('#ram').text('');
  56.                         $('#ram').prepend("<img src='/static/img/loading.gif' />")
  57.                         $('#disk').text('');
  58.                         $('#disk').prepend("<img src='/static/img/loading.gif' />")
  59.                         Messenger().run({
  60.                             errorMessage: 'Disconnected from Server',
  61.                             action: function(opts) {
  62.                                 if(connect == 0) {
  63.                                     connect = 1;
  64.                                     return opts.error({
  65.                                         status: 500,
  66.                                         readyState: 0,
  67.                                         responseText: 0
  68.                                     });
  69.                                 }
  70.                                 ws = new WebsocketClass("ws://127.0.0.1:8000/_socket_system");
  71.                                 ws.initWebsocket();
  72.                                 ws.initAutoSend();
  73.                                 reconnecting_attempt = false;
  74.                                 return opts.success();
  75.                             }
  76.                         });
  77.                     }
  78.                }
  79.         };
  80.  
  81.         // Initialize a new websocket (the first one)
  82.         var ws = new WebsocketClass("ws://127.0.0.1:8000/_socket_system");
  83.         window.setTimeout('ws.initWebsocket()', 500);
  84.         ws.initAutoSend();
  85.  
  86.         // Before changing the page, we close the socket correctly.
  87.         window.onbeforeunload = function() {
  88.             ws.socket.onclose = function () {};
  89.             ws.socket.close()
  90.         };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement