获取在线用户的名称连接到服务器 [英] Get names of Online users connected to a Server

查看:299
本文介绍了获取在线用户的名称连接到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来asp.net。我曾经使用过此<一个走了href=\"http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx\"相对=nofollow> 链接 这表明如何计算连接使用asp.net服务器的在线用户。 (这是工作,当我试图的)

我的问题是:我应该在code(Global.asax中)改变什么,这样它显示所连接的用户,而不是指望他们所有的名字。

我创建了 JS存储连接的用户名在变量 chatUsername 聊天应用文件作为如下图所示:

js文件

  VAR chatUsername = window.prompt(请输入用户名:,);
   //
   chat.client.addMessage = //函数
   //
   chat.server.send(chatUsername);

.aspx.cs文件

  //使用SignalR(我认为这并不重要)
公共类聊天:集线器
{
    公共无效发送(从字符串)
    {
        //调用所有客户端的方法addMessage方法
        Clients.All.addMessage(从);
    }
}

您可以找到我的完整code 这里

编辑:请提供相关的只有 asp.net一个简单的例子 signalr (无其他技术,如MVC )

请帮忙。


解决方案

编辑:以下code指的SignalR V0.5,而不是最新1.0Alpha2,但我相信的理由是一样的。

要做到这一点,你需要几个步骤添加到您的SignalR连接过程中,无论是在服务器和客户端:

在服务器端:


  1. 在应用程序启动,例如,你可以实例化一个静态内存库(可以是一个字典)将作为用户存储库来存储所有当前连接的用户。

  2. 在集线器,你需要处理断开事件(当用户断开连接时,需要从用户存储库以及删除),并通知所有其他客户端,该用户断开连接

  3. 在轮毂需要添加两个新的方法(该名称可以是任何你想要的),这将有助于客户端连接到系统,并得到当前连接的用户列表:

    1. GetConnectedUsers()只返回连接用户的集合

    2. 加入()式集线器将创建一个新用户,使用存储在往返的状态(由客户选择的用户名)的信息和SignalR连接ID,和新创建的用户添加到内存资料库。


在客户端:
首先,你需要实例,涉及到你的服务器端枢纽JavaScript对象

  VAR聊天= $ .connection.chat;
chat.username = chatUsername;

然后实现将由毂被调用并最终连接到集线器的所有功能:

  //第1步:启动连接
    //第2步:获取所有currenlty连接的用户
    //第3步:加入到聊天,并通知所有客户端(包括我)是有连接的新用户
    $ .connection.hub.start()
                .done(函数(){
                    chat.getConnectedUsers()
                                .done(/ *显示通讯录* /);
                                    });
                                })。完成(功能(){
                                    chat.joined();
                                });
                });
});

如果你问我们为什么需要添加一个阶段,如chat.joined(),是因为在被处理的连接事件集线器的方法,往返状态尚未公布,所以轮毂不能检索由用户选择的用户名

无论如何,我做了一个博客帖子来显示更多详细介绍如何创建使用Asp.Net MVC基本SignalR聊天的Web应用程序,它可在:
<一href=\"http://thewayof$c$c.word$p$pss.com/2012/07/24/chatr-just-another-chat-application-using-signalr/\" rel=\"nofollow\">http://thewayof$c$c.word$p$pss.com/2012/07/24/chatr-just-another-chat-application-using-signalr/

在后,你还会发现一个链接到源发布GitHub的信息库。
我希望这有助于。

瓦莱里奥

I am new to asp.net. I have gone through this link which has shown how to count the online users connected to a server using asp.net. (which is working when I tried)

My question is: What should I change in that code (Global.asax) so that It shows all the names of the connected users instead of counting them.

I created a chat application which stores the name of the connected user in a variable chatUsername in js file as shown below:

js file

   var chatUsername = window.prompt("Enter Username:", "");
   //
   chat.client.addMessage =  //Function
   //
   chat.server.send(chatUsername); 

.aspx.cs file

//Using SignalR (I think this doesnt matter)
public class Chat : Hub
{
    public void Send(string from)
    {
        // Call the addMessage method on all clients    
        Clients.All.addMessage(from);
    }
}

You can find my complete code here

EDIT: Please provide a simple example related only to asp.net or signalr (no other technologies like MVC)

Please help.

解决方案

Edit: following code refers to SignalR v0.5, not the latest 1.0Alpha2, but I believe the reasoning is the same

To do this you need to add several steps to your SignalR connection process, both in the server and in the client:

on the server side:

  1. on application start-up, for example, you can instantiate a static in-memory repository (can be a dictionary of ) that will serve as the user repository to store all currently connected users.
  2. In the hub you need to handle the Disconnect event (when a user disconnects, needs to be removed from the user repository as well) and notify all other clients that this user disconnected
  3. In the hub you need to add two new methods (the names can be whatever you want) that will help client connect to the system and get the list of currently connected users:

    1. GetConnectedUsers() that just returns a collection of connected users
    2. Joined() where the Hub will create a new User, using the info stored in the round-trip state (the username selected by the client) and the SignalR connection ID, and add the newly created user to the in-memory repository.

on the client side: First you need to instantiate the javascript object that relates to your server-side hub

var chat = $.connection.chat;
chat.username = chatUsername;

Then implements all the functions that will be called by the hub and finally connect to the hub:

    // Step 1: Start the connection
    // Step 2: Get all currenlty connected users
    // Step 3: Join to the chat and notify all the clients (me included) that there is a new user connected
    $.connection.hub.start()
                .done(function () {
                    chat.getConnectedUsers()
                                .done(/*display your contacts*/);
                                    });
                                }).done(function () {
                                    chat.joined();
                                });
                });
});

If you are asking why we need to add a stage like "chat.joined()" is because in the method on the Hub that is handling the connection event, the round-trip state is not yet available, so the hub cannot retrieve the username chosen by the user.

Anyway I made a blog post to show more in detail how to create a basic SignalR chat web application using Asp.Net MVC, and it is available at: http://thewayofcode.wordpress.com/2012/07/24/chatr-just-another-chat-application-using-signalr/

In the post you will also find a link to the github repository where the source is published. I hope this helps.

Valerio

这篇关于获取在线用户的名称连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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