简体   繁体   English

尝试创建一个信号R聊天应用程序但出现错误

[英]Trying to create a signal R chat application but gives error

I am trying to create a signal R chat application where I want user who send message should be the only to recieve the message not everyone should recieve the message but dont know where I am doing wrong as message isnot recieved by any one. 我正在尝试创建一个信号R聊天应用程序,在该应用程序中,我希望发送消息的用户应该是唯一接收消息的人,而不是每个人都应该接收该消息,但不知道我在哪里做错了,因为任何人都不会收到消息。

My code is like this: Hub Class 我的代码是这样的:Hub类

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);

        }

    }
}

index page is like this: 索引页是这样的:

<%@ 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>:&nbsp;&nbsp;' + 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>

Your question: I want user who send message should be the only to recieve the message not everyone should recieve the message. 您的问题:我希望发送消息的用户应该是唯一收到消息的用户,而不是每个人都应该收到消息。

To solve this, modify ChatHub class like this. 为了解决这个问题,像这样修改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.
}

When a client invokes a function on the server side you can retrieve their connection ID via Context.ConnectionId . 当客户端在服务器端调用函数时,可以通过Context.ConnectionId检索其连接ID。

Helpful links - http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#selectingclients 有用的链接-http: //www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#selectingclients

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM