diff --git a/js/key_worker.js b/js/key_worker.js index abff8c6d..8ef9e9e7 100644 --- a/js/key_worker.js +++ b/js/key_worker.js @@ -38430,11 +38430,13 @@ TextSecureWebSocket = function (url) { var reconnectSemaphore = 0; var reconnectTimeout = 1000; var socket; + var calledClose = false; var socketWrapper = { onmessage : function() {}, onclose : function() {}, onerror : function() {}, - getStatus : function() { return socket.readyState; } + getStatus : function() { return socket.readyState; }, + close : function() { calledClose = true; } }; var error; @@ -38455,7 +38457,7 @@ TextSecureWebSocket = function (url) { }; function onclose(e) { - if (!error) { + if (!error && !calledClose) { reconnectSemaphore--; setTimeout(connect, reconnectTimeout); } diff --git a/js/libtextsecure.js b/js/libtextsecure.js index bf818e33..67fd94f5 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -38429,11 +38429,13 @@ TextSecureWebSocket = function (url) { var reconnectSemaphore = 0; var reconnectTimeout = 1000; var socket; + var calledClose = false; var socketWrapper = { onmessage : function() {}, onclose : function() {}, onerror : function() {}, - getStatus : function() { return socket.readyState; } + getStatus : function() { return socket.readyState; }, + close : function() { calledClose = true; } }; var error; @@ -38454,7 +38456,7 @@ TextSecureWebSocket = function (url) { }; function onclose(e) { - if (!error) { + if (!error && !calledClose) { reconnectSemaphore--; setTimeout(connect, reconnectTimeout); } diff --git a/libtextsecure/test/websocket_test.js b/libtextsecure/test/websocket_test.js index 75946ea5..e691b973 100644 --- a/libtextsecure/test/websocket_test.js +++ b/libtextsecure/test/websocket_test.js @@ -15,47 +15,45 @@ */ describe('TextSecureWebSocket', function() { - var WebSocket = window.WebSocket; + var RealWebSocket = window.WebSocket; before(function() { window.WebSocket = MockSocket; }); - after (function() { window.WebSocket = WebSocket; }); - it('connects a websocket', function(done) { + after (function() { window.WebSocket = RealWebSocket; }); + it('connects and disconnects', function(done) { var mockServer = new MockServer('ws://localhost:8080'); mockServer.on('connection', function(server) { - server.on('message', function(data) { - server.send('hello'); - }); - }); - - var socket = new TextSecureWebSocket('ws://localhost:8080'); - socket.onmessage = function(response) { - assert.strictEqual(response.data, 'hello'); + socket.close(); + server.close(); done(); - }; - socket.send('data'); + }); + var socket = new TextSecureWebSocket('ws://localhost:8080'); }); it('sends a keepalive once a minute', function(done) { this.timeout(60000); - var mockServer = new MockServer('ws://localhost:8080'); + var mockServer = new MockServer('ws://localhost:8081'); mockServer.on('connection', function(server) { server.on('message', function(data) { var message = textsecure.protobuf.WebSocketMessage.decode(data); assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST); assert.strictEqual(message.request.verb, 'GET'); assert.strictEqual(message.request.path, '/v1/keepalive'); + socket.close(); + server.close(); done(); }); }); - var socket = new TextSecureWebSocket('ws://localhost:8080'); + var socket = new TextSecureWebSocket('ws://localhost:8081'); }); it('reconnects', function(done) { this.timeout(60000); - var mockServer = new MockServer('ws://localhost:8080'); - var socket = new TextSecureWebSocket('ws://localhost:8080'); + var mockServer = new MockServer('ws://localhost:8082'); + var socket = new TextSecureWebSocket('ws://localhost:8082'); socket.onclose = function() { - var mockServer = new MockServer('ws://localhost:8080'); - mockServer.on('connection', function() { + var mockServer = new MockServer('ws://localhost:8082'); + mockServer.on('connection', function(server) { + socket.close(); + server.close(); done(); }); }; diff --git a/libtextsecure/websocket.js b/libtextsecure/websocket.js index c7421d06..1366283c 100644 --- a/libtextsecure/websocket.js +++ b/libtextsecure/websocket.js @@ -28,11 +28,13 @@ TextSecureWebSocket = function (url) { var reconnectSemaphore = 0; var reconnectTimeout = 1000; var socket; + var calledClose = false; var socketWrapper = { onmessage : function() {}, onclose : function() {}, onerror : function() {}, - getStatus : function() { return socket.readyState; } + getStatus : function() { return socket.readyState; }, + close : function() { calledClose = true; } }; var error; @@ -53,7 +55,7 @@ TextSecureWebSocket = function (url) { }; function onclose(e) { - if (!error) { + if (!error && !calledClose) { reconnectSemaphore--; setTimeout(connect, reconnectTimeout); }