0
我有這個SignalR代碼。如何使用部署的服務創建SignalR客戶端?
<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>
}
此代碼部署爲雲服務湛藍。
我需要創建相同的客戶端。
我現在所做的一切。
public class Program : Hub
{
ClientEntry objClient = new ClientEntry();
string userName = "test";
string tags = "swe;India;xyz";
public static void Main(string[] args)
{
//Program prg = new Program();
//prg.ConnectToHub();
}
public void ConnectToHub()
{
var hubUrl = "http://test.cloudapp.net/Signalr/hubs/";
var hubConnection = new HubConnection(hubUrl);
var chatHubProxy = hubConnection.CreateHubProxy("TestHub");
hubConnection.Start();
}
public void addNewMessageToPage(string name, string message)
{ }
}
我要消耗addNewMessageToPage的方法,但無法理解plz幫助
問題已經解決,網址是不正確的應該是這樣(「var hubUrl =」http://test.cloudapp.net/Signalr「)。 – Sudhir
如果您嘗試其他問題,則可以使用hubConnection.TraceLevel = TraceLevels.All啓用SignalR跟蹤。 hubConnection.TraceWriter = Console.Out;' –