/editor/core/socket.js
JavaScript | 52 lines | 42 code | 7 blank | 3 comment | 2 complexity | 5c2f57243d236e2f489df31a967a6b81 MD5 | raw file
Possible License(s): Apache-2.0
- define([
- "hr/hr",
- "sockjs"
- ], function(hr, sockjs) {
- var logging = hr.Logger.addNamespace("socket");
- var Socket = hr.Class.extend({
- initialize: function() {
- var that = this;
- Socket.__super__.initialize.apply(this, arguments);
- logging.log("connecting to service", this.options.service);
- this.sock = new SockJS(window.location.origin+window.location.pathname+"socket/"+this.options.service);
- this.sock.onopen = function() {
- that.trigger("open");
- };
- this.sock.onmessage = function(e) {
- var data = JSON.parse(e.data);
- that.trigger('data', data);
- if (data.method) {
- that.trigger('do:'+data.method, data.data || {});
- }
- };
- this.sock.onclose = function() {
- that.trigger("close");
- };
- },
- // Send a message
- send: function(message) {
- this.sock.send(JSON.stringify(message));
- return this;
- },
- // Call a method
- do: function(method, data) {
- return this.send({
- 'method': method,
- 'data': data
- });
- },
- // Close the connection
- close: function() {
- this.sock.close();
- return this;
- }
- });
- return Socket;
- });