Skip to content
This repository was archived by the owner on Oct 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dist/really.0.0.1.min.js

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions dist/really.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Really.js v0.0.1
* Copyright (C) 2014-2015 Really Inc. <http://really.io>
*
* Date: Tue Jan 06 2015 17:38:48
* Date: Sun Jan 18 2015 17:08:07
*/
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Really=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// shim for using process in browser
Expand Down Expand Up @@ -9137,27 +9137,32 @@ module.exports = CallbacksBuffer;


},{"./protocol":10,"./really-error":13,"lodash":3}],8:[function(require,module,exports){
var Heartbeat, Logger, Q, logger, protocol;
var Emitter, Heartbeat, Logger, Q, logger, protocol;

protocol = require('./protocol');

Q = require('q');

Emitter = require('component-emitter');

Logger = require('./logger');

logger = new Logger();

Heartbeat = (function() {
var _ping;

function Heartbeat(interval, timeout) {
function Heartbeat(websocket, interval, timeout) {
this.websocket = websocket;
this.interval = interval != null ? interval : 5e3;
this.timeout = timeout != null ? timeout : 5e3;
logger.debug("Heartbeat: initialize with interval: " + interval + " and timeout: " + timeout);
if (!this.websocket) {
throw Error('websocket should be passed');
}
logger.debug("Heartbeat: initialize with interval: " + this.interval + " and timeout: " + this.timeout);
}

Heartbeat.prototype.start = function(websocket) {
this.websocket = websocket;
Heartbeat.prototype.start = function() {
_ping.call(this);
return logger.debug("Heartbeat: started interval: " + this.interval + " timeout: " + this.timeout);
};
Expand All @@ -9170,6 +9175,7 @@ Heartbeat = (function() {
return function(data) {
var lag, now;
now = Date.now();
console.log(data.timestamp);
lag = (now - data.timestamp) * 0.001;
logger.debug("Heartbeat: lag " + lag + " second(s)");
return Q.delay(_this.interval).then(function() {
Expand All @@ -9178,13 +9184,20 @@ Heartbeat = (function() {
});
};
})(this);
error = function() {
logger.debug('Heartbeat: lag exceed');
return this.websocket.disconnect();
};
error = (function(_this) {
return function(error) {
logger.debug('Heartbeat: lag exceed', error);
return _this.stop();
};
})(this);
return pingPromise.timeout(this.interval + this.timeout).then(success, error);
};

Heartbeat.prototype.stop = function() {
this.websocket.socket.close();
return this.websocket.emit('heartbeat:lag');
};

return Heartbeat;

})();
Expand All @@ -9193,7 +9206,7 @@ module.exports = Heartbeat;



},{"./logger":9,"./protocol":10,"q":4}],9:[function(require,module,exports){
},{"./logger":9,"./protocol":10,"component-emitter":2,"q":4}],9:[function(require,module,exports){
var Logger, _,
__slice = [].slice;

Expand Down Expand Up @@ -9281,7 +9294,7 @@ VERSION = '0';
module.exports = {
clientVersion: VERSION,
commands: {
init: 'init',
init: 'initialization',
create: 'create',
read: 'read',
get: 'get',
Expand Down Expand Up @@ -10044,8 +10057,8 @@ WebSocketTransport = (function(_super) {
var heartbeat;
_this.initialized = true;
_.flush.call(_this);
heartbeat = new Heartbeat(_this.options.heartbeatInterval, _this.options.heartbeatTimeout);
heartbeat.start(_this);
heartbeat = new Heartbeat(_this, _this.options.heartbeatInterval, _this.options.heartbeatTimeout);
heartbeat.start();
return _this.emit('initialized', data);
};
})(this);
Expand Down
25 changes: 17 additions & 8 deletions src/heartbeat.coffee
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
protocol = require './protocol'
Q = require 'q'
Emitter = require 'component-emitter'
Logger = require './logger'
logger = new Logger()

class Heartbeat
constructor: (@interval = 5e3, @timeout = 5e3) ->
logger.debug "Heartbeat: initialize with interval: #{interval} and timeout: #{timeout}"
constructor: (@websocket, @interval = 5e3, @timeout = 5e3) ->
throw Error('websocket should be passed') unless @websocket

logger.debug "Heartbeat: initialize with interval: #{@interval} and timeout: #{@timeout}"


start: (@websocket) ->
start: () ->
_ping.call(this)
logger.debug "Heartbeat: started interval: #{@interval} timeout: #{@timeout}"

_ping = () ->
message = protocol.heartbeatMessage()
pingPromise = @websocket.send message

success = (data) =>
now = Date.now()
console.log data.timestamp
lag = (now - data.timestamp) * 0.001
logger.debug "Heartbeat: lag #{lag} second(s)"
Q.delay(@interval).then =>
logger.debug "Heartbeat interval: #{@interval} timeout: #{@timeout}"
_ping.call(this)

error = () ->
logger.debug 'Heartbeat: lag exceed'
@websocket.disconnect()
error = (error) =>
logger.debug 'Heartbeat: lag exceed', error
@stop()

pingPromise.timeout(@interval + @timeout).then success, error

stop: () ->
@websocket.socket.close()
@websocket.emit 'heartbeat:lag'


module.exports = Heartbeat

2 changes: 1 addition & 1 deletion src/protocol.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports =
clientVersion: VERSION

commands:
init: 'init'
init: 'initialization'
create: 'create'
read: 'read'
get: 'get'
Expand Down
118 changes: 118 additions & 0 deletions tests/heartbeat-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
Heartbeat = require('../src/heartbeat')
protocol = require '../src/protocol'
Q = require 'q'

describe 'Heartbeat', ->
websocket =
send: (message) ->
deferred = Q.defer()
return deferred.promise

describe 'initialization', ->
it 'should set default interval time and timeout when no parameter passed', ->
heartbeat = new Heartbeat(websocket)
expect(heartbeat.interval).toEqual 5e3
expect(heartbeat.timeout).toEqual 5e3

it 'should take websocket as a parameter', ->
expect ->
heartbeat = new Heartbeat()
.toThrow Error('websocket should be passed')

describe 'start', ->
it 'should create heartbeat message', ->
heartbeat = new Heartbeat(websocket)
spyOn(protocol, 'heartbeatMessage').and.callFake ->
message =
heartbeat: true

heartbeat.start(websocket)
expect(protocol.heartbeatMessage).toHaveBeenCalled()

it 'should send heartbeat message', ->
heartbeat = new Heartbeat(websocket)
spyOn(heartbeat.websocket, 'send').and.callFake ->
deferred = Q.defer()
return deferred.promise

heartbeat.start()
expect(heartbeat.websocket.send).toHaveBeenCalledWith jasmine.any(Object)


it 'should raise promise success when resolved', (done) ->
heartbeat = new Heartbeat(websocket,1000,1000)
spyOn(heartbeat.websocket, 'send').and.callFake () ->
deferred = Q.defer()
deferred.resolve
timestamp: 123
return deferred.promise

heartbeat.start()
setTimeout( () ->
expect(heartbeat.websocket.send.calls.count()).toEqual 2
done()
, 1500)

it 'should raise promise error when timeout', (done) ->
heartbeat = new Heartbeat(websocket, 1000, 1000)
spyOn(heartbeat, 'stop').and.callThrough()
heartbeat.start()

setTimeout( () ->
expect(heartbeat.stop).toHaveBeenCalled()
done()
, 2500)

it 'should raise promise error when rejected', (done) ->
websocket =
send: (message) ->
deferred = Q.defer()
deferred.reject 'ERROR'
return deferred.promise

heartbeat = new Heartbeat(websocket, 1000, 1000)

spyOn(heartbeat, 'stop')

heartbeat.start()
setTimeout( () ->
expect(heartbeat.stop).toHaveBeenCalled()
done()
, 0)

describe 'stop', ->
it 'should close websocket', (done) ->
websocket =
send: (message) ->
deferred = Q.defer()
deferred.reject 'ERROR'
return deferred.promise
socket:
close: () -> true

heartbeat = new Heartbeat(websocket, 1000, 1000)
spyOn(heartbeat.websocket.socket, 'close')
heartbeat.start()
setTimeout( () ->
expect(heartbeat.websocket.socket.close).toHaveBeenCalled()
done()
, 0)

it 'should fire event on websocket', (done) ->
websocket =
emit: (msg) -> true
send: (message) ->
deferred = Q.defer()
deferred.reject 'ERROR'
return deferred.promise
socket:
close: () -> true


heartbeat = new Heartbeat(websocket, 1000, 1000)
spyOn(heartbeat.websocket, 'emit')
heartbeat.start()
setTimeout( () ->
expect(heartbeat.websocket.emit).toHaveBeenCalled()
done()
, 0)
2 changes: 1 addition & 1 deletion tests/protocol-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe 'protocol', ->
expect(message).toEqual
kind: 'initialization'
data:
cmd: 'init'
cmd: 'initialization'
accessToken: 'xxwmn93p0h'

describe 'createMessage', ->
Expand Down