首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态创建网站实例

动态创建网站实例
EN

Stack Overflow用户
提问于 2016-03-02 15:46:56
回答 1查看 424关注 0票数 0

我的计划是创建一个简单的聊天网站。我正在使用asp.net网页与剃刀和signalR聊天。我已经完成了默认页面和聊天室页面,但我不知道如何“动态”创建聊天室页面的多个实例,以便/ chatroom /1是与/chatroom/2不同的聊天室。

我假设这可以通过制作10个聊天室页面,并从1-10命名,但我认为这是不好的做法。我已经完成了路由,以便打开聊天室页面的一个实例,但是我不知道如何使它们彼此分离。如果需要代码,我可以将其上传到github。

编辑:Github

路由:

代码语言:javascript
复制
@using System.Web.Routing;
@{
    RouteTable.Routes.MapWebPageRoute("{chatroom}/{number}", "~/room.cshtml", 
        constraints: new { chatroom = "room", number = "[1-9]"});
}

room.cshtml

代码语言:javascript
复制
@{
    Layout = "~/_Layout.cshtml";

    string s = Request.Url.AbsolutePath;
    var RoomNumber = s.Substring(s.LastIndexOf("/") +1);
}


<div id="chat">
    <textarea id="chatBox" rows="40" cols="50" readonly="readonly"></textarea>
    <input type="text" id="message" placeholder="Your message"/>
    <input type="button" id="msgSend" value="Send" />
</div>

<script type="text/javascript">
    //simulate msgSend button with enter press when textbox focused
    $('#message').bind('keyup', function (e) {
        if (e.keyCode === 13) { // 13 is enter key
            $('#msgSend').click();
        }

    });
        $(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.broadcastMessage = function (message) {
                // Html encode display message. 
                var encodedMsg = $('<div />').text(message).html();
                //get current time
                var currentdate = new Date();
                var datetime =
                +currentdate.getHours() + ":"
                + currentdate.getMinutes() + ":"
                + currentdate.getSeconds();
                // Add the message to the page. 
                $('#chatBox').append('\n'+datetime+" "+encodedMsg);
            };
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#msgSend').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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-04 00:48:24

如果有人发生过这样的问题,那么解决它的办法就是SignalR分组。

SignalR多聊天室

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35751901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档