fkchang / opal-hot-reloader

Hot code reloading for Opal apps, includes React.rb support
MIT License
17 stars 8 forks source link

If socket is not opened on local host you have to ping to keep things alive #12

Closed catmando closed 7 years ago

catmando commented 7 years ago

This patch seems to take of the problem:

class OpalHotReloader
  def connect_to_websocket(port)
    host = `window.location.host`.sub(/:\d+/, '')
    host = '127.0.0.1' if host == ''
    protocol = `window.location.protocol` == 'https:' ? 'wss:' : 'ws:'
    ws_url = "#{host}:#{port}"
    puts "Hot-Reloader connecting to #{ws_url}"
    ws = `new WebSocket(#{protocol} + '//' + #{ws_url})`
    `#{ws}.onmessage = #{lambda { |e| reload(e) }}`
    # every(5) { `#{ws}.ping()` } 
    every(5) { `#{ws}.send('')` } # seems like ping is not implemented
  end
end