<commit_before>/*
 * DermShare -- websocket support for mobile camera
 *
 * Copyright (c) 2012-2015 Carnegie Mellon University
 * All rights reserved.
 *
 * This software is distributed under the terms of the Eclipse Public
 * License, Version 1.0 which can be found in the file named LICENSE.
 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
 */

function _ImageSocket(args) {
  var self = this;

  $.extend(this, {
    connected: ko.observable(false),
    closed: ko.observable(null),

    send_msg: function(type, data) {
      var msg = $.extend({}, data || {}, {
        type: type,
      });
      self.sock.send(JSON.stringify(msg));
    },
  });

  function connect() {
    if (args.url) {
      self.sock = new WebSocket(args.url);
    } else {
      self.sock = {};
      self.closed('WebSockets not configured');
    }

    $.extend(self.sock, {
      onopen: function(ev) {
        self.connected(true);
        if (args.open) {
          args.open(ev);
        }
      },

      onmessage: function(ev) {
        args.message(ev);
      },

      onclose: function(ev) {
        if (args.reopen && args.reopen(ev)) {
          self.connected(false);
          setTimeout(connect, 1000);
        } else {
          self.closed(ev.reason || 'Connection closed');
          if (args.close) {
            args.close(ev);
          }
        }
      },
    });
  }
  connect();

  var ping_timer = setInterval(function() {
    if (self.closed()) {
      clearInterval(ping_timer);
    } else if (self.connected()) {
      self.send_msg('ping');
    }
  }, 30000);
}


function ClientSocket(url, barcode, image, imageSaved) {
  if (!(this instanceof arguments.callee)) {
    throw "Constructor called without new";
  }

  var EXPECT_MSG = 0;
  var EXPECT_BARCODE = 1;
  var EXPECT_IMAGE = 2;

  var self = this;
  var expect;
  var image_type;

  function init_socket() {
    expect = EXPECT_BARCODE;
    self.verifier(null);
  };

  function send_saved() {
    if (self.connected() && !self.closed()) {<commit_msg>Fix QR code flicker after client websocket reconnection<commit_after>/*
 * DermShare -- websocket support for mobile camera
 *
 * Copyright (c) 2012-2015 Carnegie Mellon University
 * All rights reserved.
 *
 * This software is distributed under the terms of the Eclipse Public
 * License, Version 1.0 which can be found in the file named LICENSE.
 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
 */

function _ImageSocket(args) {
  var self = this;

  $.extend(this, {
    connected: ko.observable(false),
    closed: ko.observable(null),

    send_msg: function(type, data) {
      var msg = $.extend({}, data || {}, {
        type: type,
      });
      self.sock.send(JSON.stringify(msg));
    },
  });

  function connect() {
    if (args.url) {
      self.sock = new WebSocket(args.url);
    } else {
      self.sock = {};
      self.closed('WebSockets not configured');
    }

    $.extend(self.sock, {
      onopen: function(ev) {
        self.connected(true);
        if (args.open) {
          args.open(ev);
        }
      },

      onmessage: function(ev) {
        args.message(ev);
      },

      onclose: function(ev) {
        if (args.reopen && args.reopen(ev)) {
          self.connected(false);
          setTimeout(connect, 1000);
        } else {
          self.closed(ev.reason || 'Connection closed');
          if (args.close) {
            args.close(ev);
          }
        }
      },
    });
  }
  connect();

  var ping_timer = setInterval(function() {
    if (self.closed()) {
      clearInterval(ping_timer);
    } else if (self.connected()) {
      self.send_msg('ping');
    }
  }, 30000);
}


function ClientSocket(url, barcode, image, imageSaved) {
  if (!(this instanceof arguments.callee)) {
    throw "Constructor called without new";
  }

  var EXPECT_MSG = 0;
  var EXPECT_BARCODE = 1;
  var EXPECT_IMAGE = 2;

  var self = this;
  var expect;
  var image_type;

  function init_socket() {
    expect = EXPECT_BARCODE;
    barcode(null);
    self.verifier(null);
  };

  function send_saved() {
    if (self.connected() && !self.closed()) {
{'fraction_non_alphanumeric': 0.13388429752066117, 'fraction_numerical': 0.01038961038961039, 'mean_word_length': 2.3969526864474737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1514602', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1309, 'n_words': 332}