【发布时间】:2020-10-08 19:13:33
【问题描述】:
我正在尝试使用 SignalR 和 Angular 发送消息,但 API 从未在后端受到攻击。根本没有状态代码,因此它永远不会到达 API 端点。集线器连接建立得很好,所以我认为它在执行this.hubConnection.invoke('NewMessage', message); 时会自动到达端点,但它似乎不起作用。我真的是 SignalR 的新手,所以我很感激任何帮助!
消息发送azure函数代码
module.exports = async function (context, req) {
return {
"target": "newMessage",
"arguments": [ req.body ]
};
};
函数.json
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "signalR",
"name": "$return",
"hubName": "chat",
"direction": "out"
}
]
}
角度服务
import { Injectable, EventEmitter } from "@angular/core";
import * as signalR from "@aspnet/signalr";
import { SignalViewModel } from "./signal-view-model";
@Injectable({
providedIn: "root"
})
export class SignalRService {
private hubConnection: signalR.HubConnection;
signalReceived = new EventEmitter<SignalViewModel>();
constructor() {
this.buildConnection();
this.startConnection();
}
private buildConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:7070/api")
.build();
};
sendMessage(message: SignalViewModel) {
this.hubConnection.invoke('NewMessage', message);
}
private startConnection = () => {
this.hubConnection
.start()
.then(() => {
console.log("Connection Started...");
this.registerSignalEvents();
})
.catch(err => {
console.log("Error while starting connection: " + err);
//if you get error try to start connection again after 3 seconds.
setTimeout(function () {
this.startConnection();
}, 3000);
});
}
private registerSignalEvents() {
this.hubConnection.on("SignalMessageReceived", (data: SignalViewModel) => {
this.signalReceived.emit(data);
});
}
}
角度分量
txtMessage: string = 'd';
uniqueID: string = new Date().getTime().toString();
messages = new Array<SignalViewModel>();
signalRMessage = new SignalViewModel();
sendMessage(): void {
if (this.txtMessage) {
console.log("Executing signalr")
this.signalRMessage = new SignalViewModel();
this.signalRMessage.clientuniqueid = this.uniqueID;
this.signalRMessage.type = "sent";
this.signalRMessage.message = this.txtMessage;
this.signalRMessage.date = new Date();
this.messages.push(this.signalRMessage);
this.signalRService.sendMessage(this.signalRMessage);
this.txtMessage = '';
}
协商功能
module.exports = async function (context, req, connectionInfo) {
context.res.json(connectionInfo);
};
协商函数的function.json
{
"disabled": false,
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "signalRConnectionInfo",
"name": "connectionInfo",
"hubName": "chat",
"direction": "in"
}
]
}
【问题讨论】:
-
您好,请同时添加您的服务器端集线器代码。
-
不要认为这是正确的 url
"http://localhost:7070/api"您希望该 url 指向您在启动时设置的中心 urlendpoints.MapHub<T>("/URL"); -
他说
The hub connection establishes fine所以我相信它是为了中心。 -
这是一个控制台日志,显示连接已建立:postimg.cc/WtxcFmbG 另外我还在上面添加了协商连接 azure 功能
-
您能否在开发人员工具的“网络”选项卡中验证数据是否实际推送到客户端(或者可能在客户端启用日志记录)。查找显示“连接?”的 URL网络内选项卡并检查带有该呼叫的消息,
标签: angular signalr azure-functions