1

我正在尝试使用带有 nodeJS 的 azure 函数运行 SignalR 服务,以便能够发送消息以做出反应。文档

其实我只是想在本地设置后端服务( SignalR + 发送消息功能)。

为此,我创建了 SignalR 函数:

module.exports = async function (context, req, connectionInfo) {
    console.log(connectionInfo)
    context.res.body = connectionInfo;
};

function.json

{
    "bindings": [
    {
        "authLevel": "anonymous",
        "type": "httpTrigger",
        "direction": "in",
        "name": "request",
        "methods": [
        "get",
        "post"
        ]
    },
    {
        "type": "http",
        "direction": "out",
        "name": "res"
    },
    {
        "type": "signalR",
        "name": "signalRMessages",
        "connectionStringSetting": "Endpoint=https://xxxx.service.signalr.net;AccessKey=XXXXXXx;Version=1.0;",
        "hubName": "XXXXX",
        "direction": "out"
    }
    ]
}

然后在我的第二个函数中,它将向 SignalR 服务发送消息:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    const updates = [{
        target: 'updated',
        arguments: [responseMessage]
    }];
    context.bindings.signalRMessages = updates;
    context.done();


}

function.json

{
    "bindings": [
    {
        "authLevel": "anonymous",
        "type": "httpTrigger",
        "direction": "in",
        "name": "req",
        "methods": [
        "get",
        "post"
        ]
    },
    {
        "type": "http",
        "direction": "out",
        "name": "res"
    },
    {
        "type": "signalRConnectionInfo",
        "name": "connectionInfo",
        "hubName": "xxxxxx", <== here I putted the SignalR service name without the domaine
        "direction": "in",
        "connectionStringSetting": "Endpoint=https://XXXXX.service.signalr.net;AccessKey=XXXXXX;Version=1.0;"
    }
    ]
}

然后我local.setting.json

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureSignalRConnectionString": "Endpoint=https://xxxxxx.service.signalr.net;AccessKey=xxxxxxxxx;Version=1.0;"
    },
    "Host": {
    "LocalHttpPort": 7072,
    "CORS": "*"
    }
}

我还设置service modeserverlessSignalR 服务设置。

现在我正在尝试通过sendmessages在浏览器中运行应该触发该signalR功能的功能来测试它

但是当我sendMessages在浏览器上运行该功能时,不会触发并且SignalR Serice functioncontext.bindings 仅包含req(没有并列在)ressendMessagesfunction.json

我的配置中是否缺少某些内容?

4

0 回答 0