C# 试图创建signal R聊天应用程序,但出现错误
我正在尝试创建一个signal R聊天应用程序,我希望发送消息的用户应该是唯一接收消息的人,不是每个人都应该接收消息,但不知道我做错了什么,因为消息没有被任何人接收 我的代码如下: 枢纽类C# 试图创建signal R聊天应用程序,但出现错误,c#,jquery,asp.net,signalr,C#,Jquery,Asp.net,Signalr,我正在尝试创建一个signal R聊天应用程序,我希望发送消息的用户应该是唯一接收消息的人,不是每个人都应该接收消息,但不知道我做错了什么,因为消息没有被任何人接收 我的代码如下: 枢纽类 using Microsoft.AspNet.SignalR; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace SignalRTutorials { publ
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SignalRTutorials
{
public class ChatHub:Hub
{
public void Send( string message)
{
// Call the broadcastMessage method to update clients.
// Clients.All.broadcastMessage(name, message);
Clients.User(HttpContext.Current.Session["Name"].ToString()).broadcastMessage(message);
}
}
}
索引页如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="SignalRTutorials.index1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>SignalR Simple Chat</title>
<style type="text/css">
.container {
background-color: #99CCFF;
border: thick solid #808080;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" runat="server" />
<ul id="discussion">
</ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js" ></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.2.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.OnMessage = function (message) {
// Html encode display name and message.
// var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page. + encodedName
$('#discussion').append('<li><strong>'
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
<% Session["Name"] = displayname.Value ;%>
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send( $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
</form>
</body>
</html>
信号员简单聊天
.集装箱{
背景色:#99CCFF;
边框:厚实线#808080;
填充:20px;
利润率:20px;
}
$(函数(){
//声明代理以引用中心。
var chat=$.connection.chatHub;
//创建一个中心可以调用以广播消息的函数。
chat.client.OnMessage=函数(消息){
//Html编码显示名称和消息。
//var encodedName=$('').text(name).html();
var encodedMsg=$('').text(message.html();
//将消息添加到页面。+encodedName
$(“#讨论”)。追加(“”
+“:”+encodedMsg+” );
};
//获取用户名并将其存储到消息前。
$('#displayname').val(提示('输入您的姓名:','');
//将初始焦点设置为消息输入框。
$(“#消息”).focus();
//启动连接。
$.connection.hub.start().done(函数(){
$('#sendmessage')。单击(函数(){
//在集线器上调用Send方法。
chat.server.send($('#message').val());
//清除文本框并重置下一条注释的焦点。
$('#message').val('.focus();
});
});
});
您的问题:我希望发送消息的用户应该是唯一接收消息的用户,而不是所有人都应该接收消息
要解决此问题,请像这样修改ChatHub
类
public void Send(string message)
{
Clients.Caller.broadcastMessage(message); // calling user will be broadcasted the message only.
// OR use below one.
Clients.Client(Context.ConnectionId).broadcastMessage(message); // Context.ConnectionId -- contains connectionid for calling user.
}
当客户端调用服务器端的函数时,您可以通过Context.ConnectionId
检索它们的连接ID
有用链接-您应该将信号器用户映射到连接。请查看此链接,它可能对您有所帮助。但是,我希望根据会话或登录用户而不是连接IDI向用户发送通知。如果用户未登录,则相应用户的信号器连接也将丢失。我建议你仔细阅读这个链接。