C# 如何使用部署的服务创建SignalR客户端?
我有这个信号机代码C# 如何使用部署的服务创建SignalR客户端?,c#,azure,signals,signalr-hub,C#,Azure,Signals,Signalr Hub,我有这个信号机代码 <div class="container"> <input type="text" id="username" /> <input type="text" id="tags" /> <input type="button" id="sendmessage" value="Register" /> <input type="hidden" id="displayname" /> <ul id="discussi
<div class="container">
<input type="text" id="username" />
<input type="text" id="tags" />
<input type="button" id="sendmessage" value="Register" />
<input type="hidden" id="displayname" />
<ul id="discussion"></ul>
</div>
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.notificationHub;
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</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.
$('#username').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.register($('#username').val(), $('#tags').val());
// Clear text box and reset focus for next comment.
//$('#message').val('').focus();
});
});
});
// This optional function html-encodes messages for display in the page.
function htmlEncode(value) {
var encodedValue = $('<div />').text(value).html();
return encodedValue;
}
</script>
我必须使用addNewMessageToPage方法,但无法理解plz帮助我认为您的代码来自: 在这个例子中,他们调用
Send
方法
public void Send(string name, string message)
{
// Call the broadcastMessage method to update clients.
Clients.All.broadcastMessage(name, message);
}
使用chat.server.send($('#displayname').val(),$('#message').val())代码>
如果要调用addNewMessageToPage
您必须使用:
chat.server.addNewMessageToPage($('#displayname').val(),$('#message').val())代码>
您的中心功能应该是:
public void AddNewMessageToPage(string name, string message)
{
//Some stuff here
}
问题已经解决,URL不正确,应该是这样的(“var hubUrl=”)。如果您尝试其他问题,您可以使用hubConnection.TraceLevel=TraceLevels.All;hubConnection.TraceWriter=控制台.Out代码>
public void AddNewMessageToPage(string name, string message)
{
//Some stuff here
}