<commit_before>'use strict';

var Primus = require('primus.io');
var UAParser = require('ua-parser-js');

module.exports = {};
module.exports.startServer = function(httpServer) {
  
  var state;
  var defaultroom = 'broadcast';
  var devices = {};
  var primus = new Primus(httpServer, { transformer: 'SockJS' });
  var uaparser = new UAParser();
  
  var getDeviceID = function(req) {
    // console.log('getDeviceID', recentip + '~' + req.headers['user-agent'])
    return recentip + '~' + req.headers['user-agent']
  }
  
  var updateDevices = function () {
    var devicelist = [];
    var keys = Object.keys(devices).sort();
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      devicelist.push(devices[key]);
    }
    primus.write({type: 'devices', data: devicelist});
  }
  
  // grab the remote IP before it disappears in the bowels of primus - key for unregistering
  // devices when they disconnect
  var recentip = null
  primus.before('name', function (req, res) {
    recentip = req.headers['x-forwarded-for'] ||
      req.connection.remoteAddress ||
      (req.socket && req.socket.remoteAddress) ||
      (req.connection.socket && req.connection.socket.remoteAddress);
  });
  
  primus.on('connection', function (spark) {
    var obj = uaparser.setUA(spark.headers['user-agent']).getResult();
    // console.log(JSON.stringify(devices))
    obj.ip = recentip;
    obj.id = spark.id
    devices[getDeviceID(spark.request)] = obj
    // console.log('device connected', getDeviceID(spark.request))
    updateDevices();
  
    if (state) {
      primus.write(state);
    }
  
    // always join the default room
    spark.join(defaultroom);
  
    spark.on('data', function (data) {
      state = data
      if (process.env.DEBUG) {
        console.log('data', JSON.stringify(data));
      }<commit_msg>[#88481372] disabling the code that writes the previous state to the socket on page refresh.<commit_after>'use strict';

var Primus = require('primus.io');
var UAParser = require('ua-parser-js');

module.exports = {};
module.exports.startServer = function(httpServer) {
  
  var state;
  var defaultroom = 'broadcast';
  var devices = {};
  var primus = new Primus(httpServer, { transformer: 'SockJS' });
  var uaparser = new UAParser();
  
  var getDeviceID = function(req) {
    // console.log('getDeviceID', recentip + '~' + req.headers['user-agent'])
    return recentip + '~' + req.headers['user-agent']
  }
  
  var updateDevices = function () {
    var devicelist = [];
    var keys = Object.keys(devices).sort();
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      devicelist.push(devices[key]);
    }
    primus.write({type: 'devices', data: devicelist});
  }
  
  // grab the remote IP before it disappears in the bowels of primus - key for unregistering
  // devices when they disconnect
  var recentip = null
  primus.before('name', function (req, res) {
    recentip = req.headers['x-forwarded-for'] ||
      req.connection.remoteAddress ||
      (req.socket && req.socket.remoteAddress) ||
      (req.connection.socket && req.connection.socket.remoteAddress);
  });
  
  primus.on('connection', function (spark) {
    var obj = uaparser.setUA(spark.headers['user-agent']).getResult();
    // console.log(JSON.stringify(devices))
    obj.ip = recentip;
    obj.id = spark.id
    devices[getDeviceID(spark.request)] = obj
    // console.log('device connected', getDeviceID(spark.request))
    updateDevices();
  
//    this will store socket values between refreshes. Disabled for now, but really should be configurable
//    if (state) {
//      primus.write(state);
//    }
  
    // always join the default room
    spark.join(defaultroom);
  
    spark.on('data', function (data) {
      state = data
      if (process.env.DEBUG) {
        console.log('data', JSON.stringify(data));
      }
{'fraction_non_alphanumeric': 0.15106052767718572, 'fraction_numerical': 0.002586652871184687, 'mean_word_length': 3.105095541401274, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3996148', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1216, 'n_words': 285}