<commit_before>var fs = require("fs");
var path = require("path");

var _ = require("underscore");

var FICSClient = require("fics");

var log = require("./logger");

var SocketHandler = function(connection) {
  this.connection = connection;

  var self = this;
  this.connection.on("data", function(rawMessage) {
    var message = JSON.parse(rawMessage);

    self[message.operation](message.params);
  });

  this.connection.on("close", function() {
    if (self.client) {
      self.client.end();
    }
  });
};

SocketHandler.prototype.login = function(params) {
  this.client = new FICSClient();

  if (process.env.NODE_ENV === "development") {
    var sessionLog = fs.createWriteStream(path.join(process.cwd(), "log", "session.log"), {
     flags: "a"
    });

    this.client.getStream().pipe(sessionLog);
  }

  this.client.on("close", _.wrap(this.login.bind(this, params), function(callback) {
    var message = "Socket closed - re-logging user for websocket: " + this.connection;

    if (process.env.NODE_ENV === "development") {
      console.log(message);
    }

    log.getLogger().info(message);

    callback();
  }));

  this.client.chat().progress(_.bind(this.sendMessage, this, "chatMessage"));

  var self = this;
  this.client.login(params)
    .then(_.bind(this.sendMessage, this, "login"))
    .fail(function() {
      self.sendMessage("loginFailure");<commit_msg>fix reference to connection<commit_after>var fs = require("fs");
var path = require("path");

var _ = require("underscore");

var FICSClient = require("fics");

var log = require("./logger");

var SocketHandler = function(connection) {
  this.connection = connection;

  var self = this;
  this.connection.on("data", function(rawMessage) {
    var message = JSON.parse(rawMessage);

    self[message.operation](message.params);
  });

  this.connection.on("close", function() {
    if (self.client) {
      self.client.end();
    }
  });
};

SocketHandler.prototype.login = function(params) {
  this.client = new FICSClient();

  if (process.env.NODE_ENV === "development") {
    var sessionLog = fs.createWriteStream(path.join(process.cwd(), "log", "session.log"), {
     flags: "a"
    });

    this.client.getStream().pipe(sessionLog);
  }

  var self = this;

  this.client.on("close", _.wrap(this.login.bind(this, params), function(callback) {
    var message = "Socket closed - re-logging user for websocket: " + self.connection;

    if (process.env.NODE_ENV === "development") {
      console.log(message);
    }

    log.getLogger().info(message);

    callback();
  }));

  this.client.chat().progress(_.bind(this.sendMessage, this, "chatMessage"));

  this.client.login(params)
    .then(_.bind(this.sendMessage, this, "login"))
    .fail(function() {
      self.sendMessage("loginFailure");
{'fraction_non_alphanumeric': 0.17569021154535677, 'fraction_numerical': 0.0, 'mean_word_length': 3.769230769230769, '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': '1517851', 'n_tokens_mistral': 901, 'n_tokens_neox': 873, 'n_words': 162}