SignalR Core正在生成Hub代理脚本,但没有添加"客户端"方法。 (服务器或客户端没有错误 - 只是不工作)
从<script src="http://localhost/signalr/hubs">
proxies['messageHub'] = this.createHubProxy('messageHub');
proxies['messageHub'].client = { };
proxies['messageHub'].server = {
handleMessage: function (receivedString) {
return proxies['messageHub'].invoke.apply(proxies['messageHub'], $.merge(["HandleMessage"], $.makeArray(arguments)));
}
};
这是服务器端的集线器:
public class MessageHub : Hub
{
public void HandleMessage(string receivedString)
{
var responseString = string.Empty;
MessageHandler.HandleMessage(receivedString, ref responseString);
Clients.All.sendMessage(responseString);
}
}
sendMessage 方法应该包含在JS文件的messageHub客户端代理中。 $。connection.messageHub.client.sendMessage未定义
仅创建(并且正常)服务器代理的 handleMessage 。
这里是SignalR的StartUp.cs包含:
ConfigureServices:
services.AddMvc(options =>
{
options.Filters.Add(new RoleFilterAttribute());
}).AddJsonOptions(options => options.SerializerSettings.ContractResolver =
new DefaultContractResolver());
services.AddSignalR(options => options.Hubs.EnableDetailedErrors = true)
配置:
app.UseWebSockets();
app.UseSignalR();
project.json:
"Microsoft.AspNetCore.Mvc": "1.0.0-*",
"Microsoft.AspNetCore.WebSockets": "1.0.0",
"Microsoft.AspNetCore.SignalR.Server": "0.2.0-*",
一些额外的工作:
1 - 在服务器端更改方法案例以查看它是否已映射:
Clients.All.SendMessage(responseString);
没用!
2 - 将客户端更改为动态映射:
var connection = $.hubConnection('http://localhost/');
var proxy = connection.createHubProxy('messageHub');
connection.start({ withCredentials: false }).done(function () { console.log("CONNECTED") });
proxy.on("sendMessage", function (result) {console.log(result);});
proxy.invoke("handleMessage", msg).done(function(result)console.log(result);});
再次只有handleMessage(服务器)工作。
答案 0 :(得分:0)
根据文档,您缺少方法名称,因此“发送所有”行应如下所示
public void HandleMessage(string receivedString)
{
var responseString = string.Empty;
MessageHandler.HandleMessage(receivedString, ref responseString);
Clients.All.SendMessage("SendMessage",responseString);
}
以下也是正确的方法
app.UseSignalR(routes =>
{
routes.Hub<MessageHub>("/messageHub");
});
最后
var connection = $.hubConnection('http://localhost/');
var proxy = connection.createHubProxy('messageHub');
connection.start({ withCredentials: false }).done(function () { console.log("CONNECTED") });
proxy.on("SendMessage", function (result) {console.log(result);});
proxy.invoke("HandleMessage", msg).done(function(result)console.log(result);});