Azure SignalR 无服务器 (JavaScript) 客户端 - 将 json 正文发送到协商 API

问题描述 投票:0回答:1

有没有办法使用 JS 客户端 SignalR 库发送 JSON 正文?

我正在尝试找到一种使用以下 JS 代码添加 JSON 正文的方法

const connection = new signalR.HubConnectionBuilder().withUrl("https://[host]/[path]",
 {headers:{"Authorization": "Bearer [token]","Content-Type": "application/json","Accept": "application/json"
 },transport: signalR.HttpTransportType.LongPolling})
.configureLogging(signalR.LogLevel.Trace)
.withAutomaticReconnect()
.build()

亲切的问候

https://learn.microsoft.com/en-us/javascript/api/@microsoft/signalr/ihttpconnectionoptions?view=signalr-js-latest

找不到提供请求正文的选项。

javascript azure-signalr
1个回答
0
投票

谢谢你的建议,我尝试了一下,成功了。

const InitializeSignalRConnection = () => {

        const headers = {
            'Authorization': 'Bearer [jwt_token]',
            'Content-Type': 'application/json',
            'Accept': 'application/json, application/problem+json; charset=utf-8'
        }

        const negotiateReq = {
            'k1': 'v1',
            'k2': 'v2'
        }

        axios.post("https://[host][path]/negotiate?negotiateVersion=1", negotiateReq, {
            headers: headers
        })
            .then((response) => {
                const connection = new signalR.HubConnectionBuilder()
                    .withUrl(response.data.url,
                        {
                            accessTokenFactory: () => response.data.accessToken,
                            transport: signalR.HttpTransportType.LongPolling
                        }
                    )
                    .configureLogging(signalR.LogLevel.Trace)
                    .withAutomaticReconnect()
                    .build()

                connection.on('Notified', receiveNotifications)

                connection.start()
                    .catch(err => console.error(err.toString()));

                return connection;

            })
    }
© www.soinside.com 2019 - 2024. All rights reserved.