Added semi-functional websocket implementation of signalR for live server list changes (seems a bit chatty though with some servers only having time remaining changing, fixed crash when a user has an active login but their profile picture was removed, fixed not explicitly saving Cache.fetch'ed items as binary 'wb' resulting in corrupt profile image.

This commit is contained in:
2021-12-26 21:23:53 -06:00
parent 52e46eced5
commit e87f14079d
4 changed files with 76 additions and 6 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -48,7 +48,8 @@ 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
# NOTE: Too many incorrect entries causes lock out (Unknown duration)
@@ -68,10 +69,15 @@ class W3DHub
end
if Store.account
Async do
internet = Async::HTTP::Internet.instance
Cache.fetch(internet, Store.account.avatar_uri)
populate_account_info
page(W3DHub::Pages::Games)
end
end
end
def populate_account_info
@host.instance_variable_get(:"@account_container").clear do

View File

@@ -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"