ฉันค่อนข้างใหม่กับ SignalR งานแรกของฉันคือสร้างแอปแชทง่ายๆ
ฉันได้เรียกดูและอ่านและในที่สุดก็สร้างเพจที่คุณมาแชทและมันก็ใช้ได้ดี
ตอนนี้ฉันต้องแสดงรายการไคลเอนต์ที่เชื่อมต่อ เพื่อให้บรรลุเป้าหมายนี้ ฉันได้เขียนโค้ดต่อไปนี้ นี่คือฮับของฉัน
public class ChatHub: Hub
{
chatEntities dc = new chatEntities();
public void Send(string message,string clientName)
{
Clients.addMessage(message,clientName);
}
// I want to save the user into my database, when they join
public void Joined(string userId,string userName)
{
CurrentChattingUsers cu = new CurrentChattingUsers();
cu.ConnectionId = userId;
cu.UserName = userName;
dc.CurrentChattingUsers.AddObject(cu);
dc.SaveChanges();
Clients.joins(userId, userName);
}
// This will return a list of connected user from my db table.
public List<ClientModel> GetConnectedUsers()
{
var query = (from k in dc.CurrentChattingUsers
select new ClientModel()
{
FullName = k.UserName,
UserId = k.ConnectionId
}).ToList();
return query;
}
}
แล้วนั่นล่ะ...อะไรนะ?? ฉันจะไปในทิศทางที่ถูกต้องหรือไม่? ถ้าฉันเป็นเช่นนั้นจะเรียกวิธีการนี้จากมุมมองได้อย่างไร? ข้อเสนอแนะที่ดีบางอย่างจะช่วยฉันได้จริงๆ ไชโย
แก้ไข:
ฉันได้เพิ่มสคริปต์ต่อไปนี้เมื่อฮับเริ่มต้น
$.connection.hub.start(function () {
chat.getConnectedUsers();
});
นี่เป็นวิธีที่ส่งคืนชื่อลูกค้าใน Hub ของฉัน
public List<ClientModel> GetConnectedUsers()
{
var data = (from k in dc.Users
select new ClientModel()
{
FullName = k.UserName
}).ToList();
Clients.loadUsers(data);
return data;
}
ใน firebug ฉันเห็นว่ามันคืนค่าดังนี้
{"State":{},"Result":[{"FullName":"mokarom","UserId":null}, {"FullName":"aka8000","UserId":null},{"FullName":"johnnyno5","UserId":null},{"FullName":"reza","UserId":null},{"FullName":"amyo","UserId":null},{"FullName":"rezatech","UserId":null}],"Id":"0","Error":null,"StackTrace":null}
แต่ฉันจะแสดงสิ่งนั้นในมุมมองของฉันได้อย่างไร ??
แก้ไข:
นี่คือมุมมองที่สมบูรณ์จนถึงตอนนี้
<script type="text/javascript">
var chat;
var myClientName
$(document).ready(function(){
myClientName = '@Request.Cookies["ChatterName"].Value';
// Created proxy
chat = $.connection.chatHub;
// Assign a function to be called by the server
chat.addMessage = onAddMessage;
// Register a function with the button click
$("#broadcast").click(onBroadcast);
$('#message').keydown(function (e) {
if (e.which == 13) { //Enter
e.preventDefault();
onBroadcast();
}
});
// Start the connection
$.connection.hub.start(function () {
chat.getConnectedUsers();
});
chat.loadUsers = function (data) {
loadUsers(data);
};
});
function onAddMessage(message,clientName) {
// Add the message to the list
$('#messages').append('<div class="chatterName">' + clientName + ' </div><div class="chatterMessage"> ' + message + '</div><div class="clear">');
}
function onBroadcast() {
// Call the chat method on the server
chat.send($('#message').val(), myClientName);
$('#message').val('');
}
function loadUsers(data) {
$('#clientList').html(data.Result[0].FullName);
}
</script>
ปัญหา: ไม่เห็นอะไรเลยที่นี่: $('#clientList').html(data.Result[0].FullName); firebug บอกว่า 'ข้อมูลไม่ได้ถูกกำหนดไว้'