class SFML::Network::Http
CSFML’s tiny HTTP/1.x client. Useful when an SFML 3 game wants to talk to a leaderboard / asset server without pulling in Net::HTTP. For anything more involved (TLS, redirects, streaming, retries, JSON), Ruby’s stdlib Net::HTTP is the better tool — this wrapper exists for parity with CSFML, not because we recommend it.
http = SFML::Network::Http.new(“localhost”, port: 8080) resp = http.send_request(method: :get, uri: “/”) resp.status #=> 200 resp.body #=> “Hello world\n”
send_request accepts: method: :get / :post / :head / :put / :delete (default :get) uri: path string (default “/”) fields: Hash of header name → value body: String body (POST/PUT) http_version: [major, minor] (default [1, 0]) timeout: SFML::Time or seconds (default 0 = no timeout)
Constants
- DEFAULT_TIMEOUT
-
Default per-call timeout.
- DEFAULT_VERSION
Public Class Methods
Source
# File lib/sfml/network/http.rb, line 29 def initialize(host, port: 0) ptr = C::Network.sfHttp_create raise NetworkError, "sfHttp_create returned NULL" if ptr.null? @handle = FFI::AutoPointer.new(ptr, C::Network.method(:sfHttp_destroy)) C::Network.sfHttp_setHost(@handle, host.to_s, Integer(port)) end
Build an HTTP client pinned to host (URL prefix). Pass port: 0 to let CSFML pick the protocol’s default port.
Public Instance Methods
Source
# File lib/sfml/network/http.rb, line 39 def send_request(method: :get, uri: "/", fields: nil, body: nil, http_version: DEFAULT_VERSION, timeout: DEFAULT_TIMEOUT) request_ptr = C::Network.sfHttpRequest_create raise NetworkError, "sfHttpRequest_create returned NULL" if request_ptr.null? begin method_idx = C::Network::HTTP_METHODS.index(method) || raise(ArgumentError, "Unknown HTTP method: #{method.inspect} " \ "(expected one of #{C::Network::HTTP_METHODS.inspect})") C::Network.sfHttpRequest_setMethod(request_ptr, method_idx) C::Network.sfHttpRequest_setUri(request_ptr, uri.to_s) C::Network.sfHttpRequest_setHttpVersion(request_ptr, Integer(http_version[0]), Integer(http_version[1])) C::Network.sfHttpRequest_setBody(request_ptr, body.to_s) if body fields&.each_pair do |name, value| C::Network.sfHttpRequest_setField(request_ptr, name.to_s, value.to_s) end t = timeout.is_a?(Time) ? timeout : Time.seconds(timeout.to_f) response_ptr = C::Network.sfHttp_sendRequest(@handle, request_ptr, t.to_native) raise NetworkError, "sfHttp_sendRequest returned NULL" if response_ptr.null? Response.send(:_take_ownership, response_ptr) ensure C::Network.sfHttpRequest_destroy(request_ptr) end end
Send a request and wait for the response. See the class doc for all the kwargs.