Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tv.HTSPSocket = Ext.extend(Ext.util.Observable, {
- constructor: function(config) {
- config = config || {};
- this.addEvents({
- hello: true,
- close: true,
- error: true,
- channelAdd: true,
- channelUpdate: true,
- channelDelete: true,
- tagAdd: true,
- tagUpdate: true,
- tagDelete: true,
- dvrEntryAdd: true,
- dvrEntryUpdate: true,
- dvrEntryDelete: true,
- eventAdd: true,
- eventUpdate: true,
- eventDelete: true,
- initialSyncCompleted: true
- });
- this.listeners = config.listeners;
- var host = document.location.hostname;
- var port = document.location.port;
- this.url = 'ws://' + host + ':' + port + '/htsp';
- this.callbacks = {};
- this.seq = 0;
- tv.HTSPSocket.superclass.constructor.call(this, config);
- },
- open: function(url) {
- url = url || this.url;
- var ws = new WebSocket(url, 'htsp');
- var helloMessage = {
- method: 'hello',
- clientname: 'websocket',
- clientversion: '0',
- htspversion: 11
- };
- var onHelloResponse = function(msg) {
- this.fireEvent('hello', msg);
- }.bind(this);
- var onData = function(m) {
- var msg;
- if(typeof m.data == 'string')
- msg = JSON.parse(m.data);
- if(!msg)
- return;
- if(msg.seq in this.callbacks) {
- var cb = this.callbacks[msg.seq];
- delete this.callbacks[msg.seq];
- cb(msg);
- } else if(msg.method) {
- this.fireEvent(msg.method, msg);
- } else {
- console.log('unhandled message');
- console.dir(msg);
- }
- }.bind(this);
- ws.addEventListener('open', function() {
- this.send(helloMessage, onHelloResponse);
- }.bind(this));
- ws.addEventListener('message', function(m) {
- onData(m);
- }.bind(this));
- ws.addEventListener('error', function(e) {
- this.fireEvent('error', e);
- }.bind(this));
- ws.addEventListener('close', function() {
- this.fireEvent('close');
- }.bind(this));
- this.ws = ws;
- },
- send: function(msg, cb) {
- cb = cb || function(){};
- msg.seq = this.seq;
- msg = JSON.stringify(msg);
- this.callbacks[this.seq] = cb;
- this.seq += 1;
- this.ws.send(msg);
- },
- enableAsync: function(config) {
- config = config || {};
- config.method = 'enableAsyncMetadata';
- this.send(config);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement