【发布时间】:2019-06-09 17:14:45
【问题描述】:
我在获取 SignalR 服务器端集线器代码以调用 JS 客户端方法时遇到问题。我收到此错误消息无法读取未定义的属性“客户端”并且找不到此文件夹 http://localhost:8087/signalr/hubs。我已经相当小心地避免了明显的陷阱,但我想我仍然忽略了一些东西,我按照此链接https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr 中的步骤进行操作。这是我的代码:
这个 ChatHub 类:
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
这个 html 页面:
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/jquery.signalR-2.4.1.min.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
console.log( 'Connection established!' );
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.SendMessage($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
这是 Startup.cs 文件:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
[assembly: OwinStartup(typeof(Consultation_WebServices.Startup))]
namespace Consultation_WebServices {
public class Startup {
public void Configuration(IAppBuilder app) {
app.MapSignalR();
}
}
}
【问题讨论】:
标签: javascript c# signalr