Advertisement
Guest User

sb

a guest
Jul 31st, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tv.HTSPSocket = Ext.extend(Ext.util.Observable, {
  2.     constructor: function(config) {
  3.     config = config || {};
  4.  
  5.     this.addEvents({
  6.         hello: true,
  7.         close: true,
  8.         error: true,
  9.         channelAdd: true,
  10.         channelUpdate: true,
  11.         channelDelete: true,
  12.         tagAdd: true,
  13.         tagUpdate: true,
  14.         tagDelete: true,
  15.         dvrEntryAdd: true,
  16.         dvrEntryUpdate: true,
  17.         dvrEntryDelete: true,
  18.         eventAdd: true,
  19.         eventUpdate: true,
  20.         eventDelete: true,
  21.         initialSyncCompleted: true
  22.     });
  23.  
  24.     this.listeners = config.listeners;
  25.  
  26.     var host = document.location.hostname;
  27.     var port = document.location.port;
  28.     this.url = 'ws://' + host + ':' + port + '/htsp';
  29.     this.callbacks = {};
  30.     this.seq = 0;
  31.  
  32.     tv.HTSPSocket.superclass.constructor.call(this, config);
  33.     },
  34.     open: function(url) {
  35.     url = url || this.url;
  36.     var ws = new WebSocket(url, 'htsp');
  37.  
  38.     var helloMessage = {
  39.         method: 'hello',
  40.         clientname: 'websocket',
  41.         clientversion: '0',
  42.         htspversion: 11
  43.     };
  44.     var onHelloResponse = function(msg) {
  45.         this.fireEvent('hello', msg);
  46.     }.bind(this);
  47.  
  48.     var onData = function(m) {
  49.         var msg;
  50.         if(typeof m.data == 'string')
  51.         msg = JSON.parse(m.data);
  52.  
  53.         if(!msg)
  54.         return;
  55.  
  56.         if(msg.seq in this.callbacks) {
  57.         var cb = this.callbacks[msg.seq];
  58.         delete this.callbacks[msg.seq];
  59.         cb(msg);
  60.         } else if(msg.method) {
  61.         this.fireEvent(msg.method, msg);
  62.         } else {
  63.         console.log('unhandled message');
  64.         console.dir(msg);
  65.         }
  66.     }.bind(this);
  67.  
  68.     ws.addEventListener('open', function() {
  69.         this.send(helloMessage, onHelloResponse);
  70.     }.bind(this));
  71.  
  72.     ws.addEventListener('message', function(m) {
  73.         onData(m);
  74.     }.bind(this));
  75.  
  76.     ws.addEventListener('error', function(e) {
  77.         this.fireEvent('error', e);
  78.     }.bind(this));
  79.  
  80.     ws.addEventListener('close', function() {
  81.         this.fireEvent('close');
  82.     }.bind(this));
  83.  
  84.     this.ws = ws;
  85.     },
  86.     send: function(msg, cb) {
  87.     cb = cb || function(){};
  88.  
  89.     msg.seq = this.seq;
  90.     msg = JSON.stringify(msg);
  91.  
  92.     this.callbacks[this.seq] = cb;
  93.     this.seq += 1;
  94.     this.ws.send(msg);
  95.     },
  96.     enableAsync: function(config) {
  97.     config = config || {};
  98.     config.method = 'enableAsyncMetadata';
  99.     this.send(config);
  100.     }
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement