用户界面:
const connection = new signalR.HubConnectionBuilder()' .withUrl("https://example.com/chatHub", {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets,
})
后端:
` services.AddSignalR(options =>`
` { options.EnableDetailedErrors = true; });``services.AddCors(options =>` `{`
` options.AddPolicy("CorsPolicy",`
` builder => builder` `AllowAnyMethod()`
` .AllowAnyHeader()`
` .SetIsOriginAllowed((host) => true)`
` .AllowCredentials());`
` }); }`
`app.UseEndpoints(endpoints => { `
` endpoints.MapHub<ChatHub>("/chatHub", options =>`
`{ options.Transports =`
`HttpTransportType.WebSockets |`
` HttpTransportType.LongPolling; });`
` app.UseCors("CorsPolicy"); `
` app.UseWebSockets();`
Nginx的:
`ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
` map $http_connection $connection_upgrade {`
` "~*Upgrade" $http_connection;`
` default keep-alive;`
` }`
upstream my_app{
ip_hash;
server localhost:5000;
}
server{
listen 443 ssl;
server_name example.com;
## SSL Configurations
ssl_certificate /etc/letsencrypt/live/example.com/cert.pem;
ssl_certificate_key etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
##Configure the SignalR endpoint
location / {
proxy_pass https://example.com/chatHub;
# configure Websockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache_bypass $http_upgrade;
proxy_set_header Connection $http_connection;
# Configuration for ServerSent Events
proxy_buffering off;
# Configuration for LongPolling
proxy_read_timeout 100s;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}