signalr多个聊天室 [英] signalr multiple chat rooms

查看:1000
本文介绍了signalr多个聊天室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划建立一个聊天应用程序,我读过,signalr是应用的最佳技术之一。

i am planning to create a chat application and i've read that signalr is one of the best technologies to apply.

我见过这样的例子,但他们只有一个聊天室。

i've seen examples of it but they only have a single chat room.

我想有多个聊天室。用户将只选择那些聊天室之一。

i want to have multiple chat rooms. the user will just choose one of those chat rooms.

虽然IM初学者,我想创造signalr一个聊天室是这样的:

although im a beginner, i think to create a single chat room in signalr is by this:

<script type="text/javascript">
$(function () {
    var connection = $.connection.communicator;
    connection.receive = function (from, msg) {
        $("#chatWindow").append("<li>" + from + ": " + msg + "</li>");
    };
    $.connection.hub.start();

    $("#btnSend").click(function () {
        connection.broadcast($("#txtName").val(), $("#txtMsg").val());
    });
});
</script>

VAR连接=单一的聊天室(我不知道)

var connection = single chat room ( i'm not sure)

所以,如果我有很多的连接(例如,连接1,连接2,连接3 ....)我可以有多个聊天室?

so if i have many connections (eg. connection1, connection2, connection3....) i can have multiple chat rooms?

再一次,我不知道这是否是正确的..请帮助我如何实现多个聊天室...

once again, i am not sure if this is correct.. please help me on how to implement multiple chat rooms...

(PS我已经看到JABBR但其code是使我的鼻子流血,你能提供简单的例子PLS)

(PS i have seen JABBR but its code is making my nose bleed, can you provide simple examples pls)

推荐答案

您不必打开多个连接,只是一个,而是利用

You don't have to open multiple connections, just one, but to use Group

public class MyHub : Hub, IDisconnect
{
    public Task Join()
    {
        return Groups.Add(Context.ConnectionId, "foo");
    }

    public Task Send(string message)
    {
        return Clients["foo"].addMessage(message);
    }

    public Task Disconnect()
    {
        return Clients["foo"].leave(Context.ConnectionId);
    }
}

一组是指一个房间,所以每次一个用户加入一个房间,你只需把用户添加到组房间,当你想广播消息,刚刚在组中发送消息给客户

One group means one room, so every time one user join one room, you just add that user to the group of that room, and when you want to broadcast message, just send message to the clients in the group

详细信息:
<一href=\"https://github.com/SignalR/SignalR/wiki/Hubs\">https://github.com/SignalR/SignalR/wiki/Hubs

希望这有助于
^^

Hope this help ^^

这篇关于signalr多个聊天室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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