<commit_before>class App.WebSocket
  _instance = undefined # Must be declared here to force the closure on the class
  @connect: (args) -> # Must be a static method
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance

  @close: (args) ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.close(args)

  @send: (args) ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.send(args)

  @auth: (args) ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.auth(args)

  @channel: ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.channel()

  @_spool: ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.spool()

# The actual Singleton class
class _webSocketSingleton extends App.Controller
  @include App.LogInclude

  queue:                    []
  supported:                true
  lastSpoolMessage:         undefined
  sentSpoolFinished:        true
  connectionKeepDown:       false
  connectionEstablished:    false
  connectionWasEstablished: false
  tryToConnect:             false
  backend:                  'websocket'
  backend_port:             ''
  client_id:                undefined
  error:                    false

  constructor: (@args) ->
    super

    # on auth, send new auth data to server
    App.Event.bind(
      'auth'
      (data) =>
        @auth()
      'ws'
    )

    # bind to send messages
    App.Event.bind(
      'ws:send'
      (data) =>
        @send(data)
      'ws'
    )

    # get spool messages after successful ws login
    App.Event.bind(
      'ws:login', =>
        @spool()
      'ws'
    )

    # get spool:sent
    App.Event.bind(
      'spool:sent'
      (data) =>

        # set timestamp to get spool messages later
        @lastSpoolMessage = data.timestamp

        # set sentSpoolFinished
        @sentSpoolFinished = true

        App.Delay.clear 'reset-spool-sent-if-not-returned', 'ws'
      'ws'
    )

    # initial connect
    @connect()

  channel: ->
    @backend

  send: (data) =>
    if @backend is 'ajax'
      @_ajaxSend(data)
    else

      # A value of 0 indicates that the connection has not yet been established.
      # A value of 1 indicates that the connection is established and communication is possible.
      # A value of 2 indicates that the connection is going through the closing handshake.
      # A value of 3 indicates that the connection has been closed or could not be opened.
      if @ws.readyState isnt 1
        @queue.push data
      else
        string = JSON.stringify(data)
        @ws.send(string)

  auth: (data) =>
    return if !@supported

    # logon websocket
    data =
      event: 'login'
      session_id: App.Config.get('session_id')
      fingerprint: App.Browser.fingerprint()
    @send(data)

  spool: =>
    return if !@sentSpoolFinished
    @sentSpoolFinished = false

    # build data to send to server
    data =
      event: 'spool'
    if @lastSpoolMessage
      data['timestamp'] = @lastSpoolMessage

    @log 'debug', 'spool', data

    # reset @sentSpoolFinished if spool:sent will not return
    reset = =>
      @sentSpoolFinished = true
    App.Delay.set reset, 60000, 'reset-spool-sent-finished-if-not-returned', 'ws'

    # ask for spool messages
    @send(data)

  close: ( params = {} ) =>
    if params['force']
      @connectionKeepDown = true

    return if @backend is 'ajax'

    @ws.close()

  ping: =>
    return if @backend is 'ajax'

    @log 'debug', 'send websocket ping'
    @send(event: 'ping')

    # check if ping is back within 2 min
    App.Delay.clear 'websocket-ping-check', 'ws'
    check = =>
      @log 'debug', 'no websocket ping response, reconnect...'
      @close()
    App.Delay.set check, 90000, 'websocket-ping-check', 'ws'

  pong: ->
    return if @backend is 'ajax'

    @log 'debug', 'received websocket pong'

    # test again after 1 min
    App.Delay.set(@ping, 60000, 'websocket-pong', 'ws')

  connect: =>

    if !window.WebSocket
      @backend = 'ajax'
      @log 'debug', 'no support of websocket, use ajax long polling'
      @_ajaxInit()
      return

    protocol = 'ws://'
    if window.location.protocol is 'https:'
      protocol = 'wss://'

    if @backend is 'websocket'
      @ws = new window.WebSocket( protocol + window.location.hostname + '/ws' )
    else if @backend is 'websocketPort'
      @backend_port = App.Config.get('websocket_port') || '6042'
      @ws           = new window.WebSocket( protocol + window.location.hostname + ':' + @backend_port + '/' )
    else
      @_ajaxInit()

    # Set event handlers.
    @ws.onopen = =>
      if @backend_port
        port = ":#{@backend_port}"
      @log 'debug', "new websocket (#{@channel()}#{port}) connection open"

      @connectionEstablished    = true
      @connectionWasEstablished = true<commit_msg>Improved websocket reconnection after wakeup.<commit_after>class App.WebSocket
  _instance = undefined # Must be declared here to force the closure on the class
  @connect: (args) -> # Must be a static method
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance

  @close: (args) ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.close(args)

  @send: (args) ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.send(args)

  @auth: (args) ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.auth(args)

  @channel: ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.channel()

  @_spool: ->
    if _instance == undefined
      _instance ?= new _webSocketSingleton
    _instance.spool()

# The actual Singleton class
class _webSocketSingleton extends App.Controller
  @include App.LogInclude

  queue:                    []
  supported:                true
  lastSpoolMessage:         undefined
  sentSpoolFinished:        true
  connectionKeepDown:       false
  connectionEstablished:    false
  connectionWasEstablished: false
  tryToConnect:             false
  backend:                  'websocket'
  backend_port:             ''
  client_id:                undefined
  error:                    false

  constructor: (@args) ->
    super

    # on auth, send new auth data to server
    App.Event.bind(
      'auth'
      (data) =>
        @auth()
      'ws'
    )

    # bind to send messages
    App.Event.bind(
      'ws:send'
      (data) =>
        @send(data)
      'ws'
    )

    # get spool messages after successful ws login
    App.Event.bind(
      'ws:login', =>
        @spool()
      'ws'
    )

    # get spool:sent
    App.Event.bind(
      'spool:sent'
      (data) =>

        # set timestamp to get spool messages later
        @lastSpoolMessage = data.timestamp

        # set sentSpoolFinished
        @sentSpoolFinished = true

        App.Delay.clear 'reset-spool-sent-if-not-returned', 'ws'
      'ws'
    )

    # initial connect
    @connect()

    # send ping after visibilitychange to check if connection is open again after wakeup
    $(document).bind('visibilitychange', =>
      console.log('visibilitychange')
      return if document.hidden
      return if !@connectionEstablished
      console.log('ping')
      @ping()
    )

  channel: ->
    @backend

  send: (data) =>
    if @backend is 'ajax'
      @_ajaxSend(data)
    else

      # A value of 0 indicates that the connection has not yet been established.
      # A value of 1 indicates that the connection is established and communication is possible.
      # A value of 2 indicates that the connection is going through the closing handshake.
      # A value of 3 indicates that the connection has been closed or could not be opened.
      if @ws.readyState isnt 1
        @queue.push data
      else
        string = JSON.stringify(data)
        @ws.send(string)

  auth: (data) =>
    return if !@supported

    # logon websocket
    data =
      event: 'login'
      session_id: App.Config.get('session_id')
      fingerprint: App.Browser.fingerprint()
    @send(data)

  spool: =>
    return if !@sentSpoolFinished
    @sentSpoolFinished = false

    # build data to send to server
    data =
      event: 'spool'
    if @lastSpoolMessage
      data['timestamp'] = @lastSpoolMessage

    @log 'debug', 'spool', data

    # reset @sentSpoolFinished if spool:sent will not return
    reset = =>
      @sentSpoolFinished = true
    App.Delay.set reset, 60000, 'reset-spool-sent-finished-if-not-returned', 'ws'

    # ask for spool messages
    @send(data)

  close: ( params = {} ) =>
    if params['force']
      @connectionKeepDown = true

    return if @backend is 'ajax'

    @ws.close()

  ping: =>
    return if @backend is 'ajax'

    @log 'debug', 'send websocket ping'
    @send(event: 'ping')

    # check if ping is back within 90 sec
    App.Delay.clear 'websocket-ping-check', 'ws'
    check = =>
      @log 'debug', 'no websocket ping response, reconnect...'
      @close()
    App.Delay.set check, 90000, 'websocket-ping-check', 'ws'

  pong: ->
    return if @backend is 'ajax'

    @log 'debug', 'received websocket pong'

    # test again after 60 sec
    App.Delay.set(@ping, 60000, 'websocket-pong', 'ws')

  connect: =>

    if !window.WebSocket
      @backend = 'ajax'
      @log 'debug', 'no support of websocket, use ajax long polling'
      @_ajaxInit()
      return

    protocol = 'ws://'
    if window.location.protocol is 'https:'
      protocol = 'wss://'

    if @backend is 'websocket'
      @ws = new window.WebSocket( protocol + window.location.hostname + '/ws' )
    else if @backend is 'websocketPort'
      @backend_port = App.Config.get('websocket_port') || '6042'
      @ws           = new window.WebSocket( protocol + window.location.hostname + ':' + @backend_port + '/' )
    else
      @_ajaxInit()

    # Set event handlers.
    @ws.onopen = =>
      if @backend_port
        port = ":#{@backend_port}"
      @log 'debug', "new websocket (#{@channel()}#{port}) connection open"

      @connectionEstablished    = true
      @connectionWasEstablished = true
{'fraction_non_alphanumeric': 0.10721951219512195, 'fraction_numerical': 0.00526829268292683, 'mean_word_length': 2.3099773974814335, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 48, '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': '3629144', 'n_tokens_mistral': 3227, 'n_tokens_neox': 3061, 'n_words': 906}