HTTP/2 服务器
通过 TsgcWebSocketHTTPServer 在单一 TLS 端口上同时提供 HTTP/2 (h2)、HTTP/1.1 和 WebSocket 服务。内置 ALPN 协商、HPACK 和流多路复用。
通过 TsgcWebSocketHTTPServer 在单一 TLS 端口上同时提供 HTTP/2 (h2)、HTTP/1.1 和 WebSocket 服务。内置 ALPN 协商、HPACK 和流多路复用。
用于 WebSocket 和 HTTP/1.1 的同一 TsgcWebSocketHTTPServer 也可通过 TLS 提供 HTTP/2 服务 — 切换 Specifications.HTTP2,其余由 ALPN 处理。
TsgcWebSocketHTTPServer
Windows, macOS, Linux, iOS, Android
Professional / Enterprise
在 TsgcWebSocketHTTPServer 实例上,启用 TLS 并切换 Specifications.HTTP2 — ALPN h2 协商自动处理升级。
uses
sgcWebSocket;
var
Server: TsgcWebSocketHTTPServer;
begin
Server := TsgcWebSocketHTTPServer.Create(nil);
Server.Port := 443;
Server.SSL := True;
Server.SSLOptions.CertFile := 'cert.pem';
Server.SSLOptions.KeyFile := 'key.pem';
// Allow HTTP/1.1, HTTP/2 and WebSocket on the same port
Server.Specifications.HTTP := True;
Server.Specifications.HTTP2 := True;
Server.Specifications.RFC6455 := True;
Server.OnCommandGet := procedure(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo)
begin
AResponseInfo.ContentText := 'Hello over HTTP/' + IfThen(ARequestInfo.HTTP2, '2', '1.1');
end;
Server.Active := True;
end;
// uses: sgcWebSocket
TsgcWebSocketHTTPServer *Server = new TsgcWebSocketHTTPServer(this);
Server->Port = 443;
Server->SSL = true;
Server->Specifications->HTTP2 = true;
Server->Active = true;
启用 HTTP/2 的 TsgcWebSocketHTTPServer — HTTP/1.1、HTTP/2 和 WebSocket 共享一个 TLS 端点。
在 TLS 握手期间,服务器同时通告 http/1.1 和 h2。客户端选择协议,服务器相应分发 — 无需单独的监听 socket。
WebSocket 客户端仍通过 HTTP/1.1 路径升级。通过 Specifications.RFC8441 支持 RFC 8441(通过 HTTP/2 引导 WebSocket)。
内置用于响应标头的 HPACK 编码器,以及将每个请求分配到其自身 stream-id 的流多路复用核心,并遵循优先级提示。
支持服务器推送(PUSH_PROMISE),但已被弃用 — 现代客户端会忽略它;建议改用 103 Early Hints 或 HTTP/3。
将 SSLOptions.IOHandler 设为 iohOpenSSL(跨平台)或 iohSChannel(Windows)。根据 RFC 7540 / 9113,HTTP/2 部署需要 TLS 1.2+。
每个 HTTP/2 连接通过 OnHTTP2Settings 公开流数量、RTT 估算、总收发字节数和协商的 SETTINGS 帧参数。