Last active
April 13, 2018 17:08
-
-
Save rbarros/ee9d6d594d29eb0a8e4fbcf2042fe019 to your computer and use it in GitHub Desktop.
Classe SagaWebSocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* Classe SagaWebSocket | |
* | |
* @author Ramon Barros [contato@ramon-barros.com] | |
* @date 2018-03-16 | |
* Copyright (c) 2018 Ramon Barros | |
*/ | |
/* jslint devel: true, unparam: true, indent: 2 */ | |
(function (window, $, Promise) { | |
'use strict'; | |
/** | |
* Inicia propriedades da classe | |
* @author Ramon Barros [contato@ramon-barros.com] | |
* @return {SagaWebSocket} | |
*/ | |
var SagaWebSocket = function(settings) { | |
var defaultSettings = { | |
'debug': false, | |
'url': 'ws://localhost:8080', | |
'protocols': null | |
}; | |
this.version = '1.0.0'; | |
this.settings = $.extend({}, defaultSettings, settings); | |
this.data = { | |
'error': false, | |
'msg': null, | |
'resourceId': null | |
}; | |
// Keep all pending requests here until they get responses | |
this.callbacks = {}; | |
// Create a unique callback ID to map requests to responses | |
this.currentCallbackId = 0; | |
return this.__constructor(); | |
}; | |
/** | |
* Construtor da classe | |
* @author Ramon Barros [contato@ramon-barros.com] | |
* @return {SagaWebSocket} | |
*/ | |
SagaWebSocket.prototype.__constructor = function() { | |
console.log('SagaWebSocket:__constructor()'); | |
return this; | |
}; | |
/** | |
* Método para instânciar a conexão com o WebSocket | |
* @return {SagaWebSocket} | |
*/ | |
SagaWebSocket.prototype.connect = function() { | |
var _this = this; | |
return new Promise(function (resolve, reject) { | |
_this.ws = new WebSocket(_this.settings.url); | |
_this.ws.onmessage = function (message) { | |
console.log("Received on websocket: " + message); | |
resolve(message); | |
}; | |
_this.ws.onerror = function (error) { | |
console.log('WebSocket error: ' + error); | |
reject(error); | |
}; | |
_this.ws.onclose = function (event) { | |
console.log("Websocket socket closed: " + JSON.stringify(event)); | |
}; | |
}); | |
}; | |
/** | |
* Envia mensagem para o servidor websocket | |
* @param request | |
*/ | |
SagaWebSocket.prototype.send = function(request) { | |
var defaultMsg = { | |
'all': false, | |
'class': null, | |
'method': null, | |
'params': [] | |
}; | |
this.msg = $.extend({}, defaultMsg, request); | |
this.ws.send(JSON.stringify(this.msg)); | |
}; | |
window.SagaWebSocket = SagaWebSocket; | |
return window.SagaWebSocket; | |
// var sagaWS = new SagaWebSocket({ | |
// 'url': 'ws://localhost:8080' | |
// }); | |
// sagaWS.connect().then(function(data) { | |
// console.log(data); | |
// }).catch(function(error) { | |
// throw error; | |
// }); | |
// sagaWS.send({ | |
// class: 'Namesapce\\Class', | |
// method: 'method', | |
// params: [] | |
// }); | |
}(this, jQuery, Promise)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment