我使用Asp.net MVC,AngularJs,SignalR和Jquery创建一个聊天应用程序。 在聊天视图中,当我尝试设置聊天对象的值时,它通过空值代码引用位于括号内(var chat = $。connection.chathub;)。没有其他功能因此而起作用。 我在这个项目中使用“Microsoft.AspNet.SignalR.2.2.2”。 jquery和signalr相关的脚本例如'jquery.signalR-2.2.2.js,jquery-ui-1.12.1.js'以及其他几个jquery库。
任何人都可以帮我吗?我附上了供您参考的代码。
@section scripts{
@*@Scripts.Render("~/Scripts/jquery-ui-1.12.1.min.js")
@Scripts.Render("~/Scripts/jquery.signalR-2.2.2.min.js")*@
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script type="text/javascript">
$(function() {
StartChat();
});
function StartChat() {
alert('StartChat');
var chat = $.connection.chathub;
alert('chat : ' + $.connection.chathub);
// Get logged in user
$('#UserIn').val($('#LoggedInUser').val());
chat.client.differentName = function (name) {
return false;
// Prompts for different user name
$('#UserIn').val($('#LoggedInUser').val());
chat.server.notify($('#UserIn').val(), $.connection.hub.id);
};
chat.client.online = function (name) {
// Update list of users
if (name == $('#UserIn').val())
$('#onlineusers').append('<div class="border" style="color:green">You: ' + name + '</div>');
else {
$('#onlineusers').append('<div class="border">' + name + '</div>');
$("#users").append('<option value="' + name + '">' + name + '</option>');
}
};
//
chat.client.enters = function (name) {
$('#chatlog').append('<div ><i>' + name + ' joins the conversation</i></div>');
$("#users").append('<option value="' + name + '">' + name + '</option>');
$('#onlineusers').append('<div class="border">' + name + '</div>');
};
// Create a function that the hub can call to broadcast chat messages.
chat.client.broadcastMessage = function (name, message) {
//display the message
$('#chatlog').append('<div class="border"><span style="color:orange">' + name + '</span>: ' + message + '</div>');
};
chat.client.disconnected = function (name) {
//Calls when someone leaves the page
$('#chatlog').append('<div ><i>' + name + ' leaves the conversation</i></div>');
$('#onlineusers div').remove(":contains('" + name + "')");
$("#users option").remove(":contains('" + name + "')");
};
// start the connection
$.connection.hub.start().done(function() {
alert('Send button clicked : ' + $('#message').val());
//Calls the notify method of the server
chat.server.notify($('#UserIn').val(), $.connection.hub.id);
$('#btnsend').click(function() {
alert('Send button clicked : ' + $('#message').val());
// Call the Send method on the hub.
chat.server.send($('#UserIn').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
})
}
</script>
}
@model ZupChatApp.Models.User
@{
ViewBag.Title = "ChatRoomView";
}
@Html.Label("LoggedInUser", Model.FirstName, new { id = "" })
<h2>Zup Chat Room View</h2>
<div class="LeftContent">
abcccc
</div>
<div class="CenterContent">
<div id="container">
<input type="hidden" id="nickname" />
<div id="chatlog"></div>
@*<div id="onlineusers">
<b>Online Users</b>
</div>*@
<div id="chatarea">
<div class="messagelog">
<textarea spellcheck="true" id="message" class="messagebox"></textarea>
</div>
<div class="actionpane">
<input type="button" id="btnsend" value="Send" class="btn btn-success col-md-offset-2 glyphicon-font" />
</div>
</div>
</div>
</div>
<div></div>
我同意adiga。不要忘记标记答案,如果它是正确的:) – toddmo
@toddmo,我也认为是这样,但它不是。任何想法在哪里,我错了吗? – t4thilina