<commit_before>/********************************************************************** 
 Freeciv - Copyright (C) 2009 - <NAME>   <EMAIL>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/


var error_shown = false;
var syncTimerId = -1;
var isWorking = false;

var clinet_last_send = 0;
var debug_client_speed_list = [];

var freeciv_version = "+Freeciv.Web.Devel-2.6-2014.Apr.02b";

var ws = null;
var civserverport = null;
var wsport = 1337;

/****************************************************************************
  Initialized the Network communication, by requesting a valid server port.
****************************************************************************/
function network_init()
{

  if (!"WebSocket" in window) {
    alert("WebSockets not supported");
    return;
  }

  var civclient_request_url = "/civclientlauncher";
  if ($.getUrlVar('action') != null) civclient_request_url += "?action=" + $.getUrlVar('action');
  if ($.getUrlVar('civserverport') != null) civclient_request_url += "&civserverport=" + $.getUrlVar('civserverport');

  $.ajax({
   type: 'POST',
   url: civclient_request_url,
   success: function(data, textStatus, request){
       civserverport = request.getResponseHeader('port');
       var connect_result = request.getResponseHeader('result');
       if (civserverport != null && connect_result == "success") {
         websocket_init(); 
         load_game_check();

       } else {
         show_dialog_message("Network error", "Invalid server port. Error: " + connect_result);
       }
   },
   error: function (request, textStatus, errorThrown) {
	show_dialog_message("Network error", "Unable to communicate with civclientlauncher servlet . Error: " 
		+ textStatus + " " + errorThrown + " " + request.getResponseHeader('result')); 
   }
  });



}

/****************************************************************************
  Initialized the WebSocket connection.
****************************************************************************/
function websocket_init()
{
  /* port 1337 - 1340 is used by the freeciv-proxy WebSocket server. */
  wsport = 1337 + Math.floor(Math.random()*4);

  ws = new ReconnectingWebSocket("ws://" + window.location.hostname + ":" + wsport + "/civsocket");

  ws.onopen = function () {
    var login_message = {"type":4, "username" : username,
    "capability": freeciv_version, "version_label": "-dev",
    "major_version" : 2, "minor_version" : 5, "patch_version" : 99,
    "port": civserverport};
    ws.send(JSON.stringify(login_message));
  };


  ws.onmessage = function (event) {
     if (typeof client_handle_packet !== 'undefined') {
       client_handle_packet(jQuery.parseJSON(event.data));
     } else {
       console.error("Error, freeciv-web not compiled correctly. Please "
             + "run sync.sh in freeciv-proxy correctly.");
     }
  };

  ws.onclose = function (event) {
   console.debug("WebSocket connection closed, reconnecting: " + event.code + ", " + event.reason); 
  };

  ws.onerror = function (evt) {
   show_dialog_message("Network error", "Unable to communicate with server using WebSockets. Error: " + evt); 
   console.error("Unable to communicate with server using WebSockets. Error: " + evt);
  };
}

/****************************************************************************
  Stops network sync.
****************************************************************************/
function network_stop()
{
  ws.close();
}

/****************************************************************************
  Sends a request to the server, with a JSON packet.
****************************************************************************/
function send_request(packet_payload) 
{
  if (ws != null) {
    ws.send(packet_payload);
  }

  if (debug_active) {
    clinet_last_send = new Date().getTime();<commit_msg>Set websocket variable to null, so that it will not be possible to send data to it once closed.<commit_after>/********************************************************************** 
 Freeciv - Copyright (C) 2009 - <NAME>   <EMAIL>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/


var error_shown = false;
var syncTimerId = -1;
var isWorking = false;

var clinet_last_send = 0;
var debug_client_speed_list = [];

var freeciv_version = "+Freeciv.Web.Devel-2.6-2014.Apr.02b";

var ws = null;
var civserverport = null;
var wsport = 1337;

/****************************************************************************
  Initialized the Network communication, by requesting a valid server port.
****************************************************************************/
function network_init()
{

  if (!"WebSocket" in window) {
    alert("WebSockets not supported");
    return;
  }

  var civclient_request_url = "/civclientlauncher";
  if ($.getUrlVar('action') != null) civclient_request_url += "?action=" + $.getUrlVar('action');
  if ($.getUrlVar('civserverport') != null) civclient_request_url += "&civserverport=" + $.getUrlVar('civserverport');

  $.ajax({
   type: 'POST',
   url: civclient_request_url,
   success: function(data, textStatus, request){
       civserverport = request.getResponseHeader('port');
       var connect_result = request.getResponseHeader('result');
       if (civserverport != null && connect_result == "success") {
         websocket_init(); 
         load_game_check();

       } else {
         show_dialog_message("Network error", "Invalid server port. Error: " + connect_result);
       }
   },
   error: function (request, textStatus, errorThrown) {
	show_dialog_message("Network error", "Unable to communicate with civclientlauncher servlet . Error: " 
		+ textStatus + " " + errorThrown + " " + request.getResponseHeader('result')); 
   }
  });



}

/****************************************************************************
  Initialized the WebSocket connection.
****************************************************************************/
function websocket_init()
{
  /* port 1337 - 1340 is used by the freeciv-proxy WebSocket server. */
  wsport = 1337 + Math.floor(Math.random()*4);

  ws = new ReconnectingWebSocket("ws://" + window.location.hostname + ":" + wsport + "/civsocket");

  ws.onopen = function () {
    var login_message = {"type":4, "username" : username,
    "capability": freeciv_version, "version_label": "-dev",
    "major_version" : 2, "minor_version" : 5, "patch_version" : 99,
    "port": civserverport};
    ws.send(JSON.stringify(login_message));
  };


  ws.onmessage = function (event) {
     if (typeof client_handle_packet !== 'undefined') {
       client_handle_packet(jQuery.parseJSON(event.data));
     } else {
       console.error("Error, freeciv-web not compiled correctly. Please "
             + "run sync.sh in freeciv-proxy correctly.");
     }
  };

  ws.onclose = function (event) {
   console.debug("WebSocket connection closed, reconnecting: " + event.code + ", " + event.reason); 
  };

  ws.onerror = function (evt) {
   show_dialog_message("Network error", "Unable to communicate with server using WebSockets. Error: " + evt); 
   console.error("Unable to communicate with server using WebSockets. Error: " + evt);
  };
}

/****************************************************************************
  Stops network sync.
****************************************************************************/
function network_stop()
{
  ws.close();
  ws = null;
}

/****************************************************************************
  Sends a request to the server, with a JSON packet.
****************************************************************************/
function send_request(packet_payload) 
{
  if (ws != null) {
    ws.send(packet_payload);
  }

  if (debug_active) {
    clinet_last_send = new Date().getTime();
{'fraction_non_alphanumeric': 0.27396011396011394, 'fraction_numerical': 0.008433048433048432, 'mean_word_length': 4.51602765556254, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, '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': '3393780', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2256, 'n_words': 647}