按照有关简单聊天中心的说明on the SignalR website进行操作,从技术上讲,我可以使其启动并运行,并且我没有对该说明进行任何修改.
但是,该应用程序第一次启动时,它显示Clients.All.broadcastMessage不能正确动态地绑定.
当客户端执行javascript chat.server.send()时,服务器的Send()方法将执行3次(请参阅更新).前两个导致错误:
Additional information: ‘Microsoft.AspNet.SignalR.Hubs.ClientProxy’
does not contain a definition for ‘broadcastMessage’
第三部分按预期执行:客户端从服务器接收广播.
客户端代码的后续执行仅导致服务器上执行一次Send()方法,并且广播消息将按预期执行.
我是否无法正确初始化某些内容?
更新:服务器代码仅在第一次执行一次,而不是三次.干扰继续按钮两次,然后起作用.并且由于请求了代码,因此是SignalR Demo上的逐字代码
但…
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<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 references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js" ></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.3.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<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 () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
</body>
</html>
和
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
}
}
解决方法:
这似乎是过度突破例外的情况.在这种情况下,Microsoft.CSharp.RuntimeBinder.RuntimeBinderException显然会在后台正常抛出,而实际上不会抛出到顶部.
解决方案是告诉调试器不要在该异常上中断.