如何使用ASP.NET信号器向服务器的每个客户端和所有客户端发送消息。 [英] How do I send message to each client and to all clients from server using ASP.NET signalr.

查看:65
本文介绍了如何使用ASP.NET信号器向服务器的每个客户端和所有客户端发送消息。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在控制台应用程序中创建了服务器,我想在winform中制作,其次我想知道有多少客户端正在连接,我选择向谁发送消息或我想向所有人发送消息客户。



请帮助我,实际上这是我在办公室的第8天,他们希望我测试我是否可以开发这个。首先是我的控制台服务器代码,下面是java脚本客户端代码



我尝试过:



i have made the server in a console application which i want to make in a winform, secondly i want to know how many clients are being connected and it is my choosing to whom i send message or i want to send message to all the clients.

please help me, actually it is my 8th day in office and they want me to test whether i can develop this or not. First is my console Server Code , following it is the code of java script client

What I have tried:

class Program
    {
        static void Main(string[] args)
        {
            string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
    public class MyHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.addMessage(name, message);
        }
    }




<!DOCTYPE html>
<html>
<head>
    <title>SignalR Simple Chat</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
    <!--Script references. -->
    
    <script src="Scripts/jquery-1.6.4.min.js"></script>
   
    <script src="Scripts/jquery.signalR-2.0.3.min.js"></script>
    
    <script src="http://localhost:8080/signalr/hubs"></script>
    
    <script type="text/javascript">
        $(function () {
            
            $.connection.hub.url = "http://localhost:8080/signalr";

           
            var chat = $.connection.myHub;

            
            chat.client.addMessage = function (name, message) {
                
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
               
                $('#discussion').append('<li>' + encodedName
                    + ':  ' + encodedMsg + '</li>');
            };
            
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                  
                    chat.server.send($('#displayname').val(), $('#message').val());
                    
                    $('#message').val('').focus();
                });
            });
        });
    </script>
</body>
</html>

推荐答案

function (){


.connection.hub.url = http:// localhost:8080 / signalr;


var chat =
.connection.hub.url = "http://localhost:8080/signalr"; var chat =


.connection.myHub;


chat.client.addMessage = function (姓名,消息){

var encodedName =
.connection.myHub; chat.client.addMessage = function (name, message) { var encodedName =


这篇关于如何使用ASP.NET信号器向服务器的每个客户端和所有客户端发送消息。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆