diff --git a/lib/api/server_list_updater.rb b/lib/api/server_list_updater.rb new file mode 100644 index 0000000..94de59e --- /dev/null +++ b/lib/api/server_list_updater.rb @@ -0,0 +1,60 @@ +class W3DHub + class Api + class ServerListUpdater + class Connection < ::Protocol::WebSocket::Connection + def read + if (buffer = super) + buffer.split("\x1e").map { |json| parse(json) } + end + end + + def read1 + read1&.first + end + + def write(object) + super(dump(object) + "\x1e") + end + end + + # TODO: Properly start up and monitor for updates to server list + def initialize + Async do |task| + headers = [["User-Agent", "Cyberarm's Websocket Testing"]] + + internet = Async::HTTP::Internet.instance + + response = internet.post("https://gsh.w3dhub.com/listings/push/v2/negotiate?negotiateVersion=1", headers, [""]) + data = JSON.parse(response.read, symbolize_names: true) + + # TODO: Replace with Api.server_list + response = internet.get("https://gsh.w3dhub.com/listings/getAll/v2?statusLevel=2", headers, [""]) + servers = JSON.parse(response.read, symbolize_names: true) + + id = data[:connectionToken] + endpoint = Async::HTTP::Endpoint.parse("https://gsh.w3dhub.com/listings/push/v2?id=#{id}", alpn_protocols: Async::HTTP::Protocol::HTTP11.names) + + Async::WebSocket::Client.connect(endpoint, headers: headers, handler: WSS::Connection) do |connection| + connection.write({ protocol: "json", version: 1 }) + connection.flush + pp connection.read + connection.write({ "type": 6 }) + + servers.each_with_index do |server, i| + i += 1 + out = { "type": 1, "invocationId": "#{i}", "target": "SubscribeToServerStatusUpdates", "arguments": [server[:id], 2] } + connection.write(out) + end + + while (message = connection.read) + connection.write({ type: 6 }) if message.first[:type] == 6 + + # TODO: process messages (of type 3?) + pp message + end + end + end + end + end + end +end diff --git a/lib/cache.rb b/lib/cache.rb index 4541ca9..df72cf0 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -15,8 +15,8 @@ class W3DHub else response = internet.get(uri, [["user-agent", W3DHub::Api::USER_AGENT]]) - if response.success?#status == 200 - response.save(path) + if response.success? + response.save(path, "wb") path end diff --git a/lib/pages/login.rb b/lib/pages/login.rb index 4fc3d79..4c325a0 100644 --- a/lib/pages/login.rb +++ b/lib/pages/login.rb @@ -48,9 +48,10 @@ class W3DHub Cache.fetch(internet, account.avatar_uri) - populate_account_info; page(W3DHub::Pages::Games) + populate_account_info + page(W3DHub::Pages::Games) else - # An error occurred, enable account entry + # An error occurred, enable account entry # NOTE: Too many incorrect entries causes lock out (Unknown duration) @username.enabled = true @password.enabled = true @@ -68,8 +69,13 @@ class W3DHub end if Store.account - populate_account_info - page(W3DHub::Pages::Games) + Async do + internet = Async::HTTP::Internet.instance + Cache.fetch(internet, Store.account.avatar_uri) + + populate_account_info + page(W3DHub::Pages::Games) + end end end diff --git a/w3d_hub_linux_launcher.rb b/w3d_hub_linux_launcher.rb index 26ba98a..5a6e777 100644 --- a/w3d_hub_linux_launcher.rb +++ b/w3d_hub_linux_launcher.rb @@ -17,6 +17,9 @@ require "async" require "async/barrier" require "async/semaphore" require "async/http/internet/instance" +require "async/http/endpoint" +require "async/websocket/client" +require "protocol/websocket/connection" I18n.load_path << Dir[File.expand_path("locales") + "/*.yml"] I18n.default_locale = :en @@ -60,6 +63,7 @@ require_relative "lib/api/service_status" require_relative "lib/api/applications" require_relative "lib/api/news" require_relative "lib/api/server_list_server" +require_relative "lib/api/server_list_updater" require_relative "lib/api/account" require_relative "lib/api/package"