【发布时间】:2019-04-10 10:52:43
【问题描述】:
在过去的 2 周里,我一直在努力让它发挥作用,但我似乎找不到问题所在。当我单击用户与之聊天时,似乎并没有在两者之间创建聊天室。我遵循了那个教程https://pusher.com/tutorials/chat-aspnet/,但编辑了一些东西,比如我的数据库。调试后,当我单击其中一个用户链接时,它似乎什么也没做。
我尝试过多次重命名所有内容并重新开始。
我认为这可能与 routeconfig 有关,或者它甚至没有在点击时调用 javascript gae。
它所做的只是将我当前的地址:localhost/chat 更改为 localhost/chat#
我的 javascript 视图:
Javascript:
// get chat data
function getChat(contact_id) {
$.get("/contact/conversations/" + contact_id)
.done(function(resp) {
let chat_data = resp.data || [];
loadChat(chat_data);
});
}
//load chat data into view
function loadChat(chat_data) {
chat_data.forEach(function(data) {
displayMessage(data);
});
$('.chat__body').show();
$('.__no__chat__').hide();
}
// select contact to chat with
$('.user__item').click(function(e) {
e.preventDefault();
currentContact = {
id: $(this).data('contact-id'),
name: $(this).data('contact-name'),
};
if (conversationChannelName) {
pusher.unsubscribe(conversationChannelName);
}
conversationChannelName = getConvoChannel((@ViewBag.currentUser.Id * 1), (currentContact.id * 1));
currentconversationChannel = pusher.subscribe(conversationChannelName);
bind_client_events();
$('#contacts').find('li').removeClass('active');
$('#contacts .contact-' + currentContact.id).find('li').addClass('active');
getChat(currentContact.id);
});
function getConvoChannel(user_id, contact_id) {
if (user_id > contact_id) {
return 'private-chat-' + contact_id + '-' + user_id;
}
return 'private-chat-' + user_id + '-' + contact_id;
}
html
<div class="__no__chat__">
<p>Select a contact to chat with</p>
</div>
<div class="panel-body users__body">
<ul id="contacts" class="list-group">
@foreach (var user in @ViewBag.allUsers)
{
<a class="user__item contact-@user.Id" href="#" data-contact-id="@user.Id" data-contact-name="@user.FirstName">
<li>
<div class="avatar">
<img src="@Url.Content("~/Content/no_avatar.png")">
</div>
<span>@user.FirstName</span>
<div class="status-bar"></div>
</li>
</a>
}
</ul>
</div>
路由配置:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Login",
url: "login",
defaults: new { controller = "Auth", action = "Index" }
);
routes.MapRoute(
name: "ChatRoom",
url: "chat",
defaults: new { controller = "Chat", action = "Index" }
);
routes.MapRoute(
name: "GetContactConversations",
url: "contact/conversations/{contact}",
defaults: new { controller = "Chat", action = "ConversationWithContact", contact = "" }
);
routes.MapRoute(
name: "PusherAuth",
url: "pusher/auth",
defaults: new { controller = "Auth", action = "AuthForChannel" }
);
routes.MapRoute(
name: "SendMessage",
url: "send_message",
defaults: new { controller = "Chat", action = "SendMessage" }
);
routes.MapRoute(
name: "MessageDelivered",
url: "message_delivered/{message_id}",
defaults: new { controller = "Chat", action = "MessageDelivered", message_id = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Auth", action = "Index", id = UrlParameter.Optional }
);
【问题讨论】:
标签: javascript html asp.net-mvc function routes