使用带有mvc的Asp.Net中的SignalR聊天应用程序

时间:2015-03-04 09:51:43

标签: javascript c# asp.net asp.net-mvc signalr

我跟着this link。在这个网站项目将工作正常。但是当我按照这个链接创建新项目时,UI将被打开但是messgae传递没有完成。所以请帮助我。

Html脚本:

@{
    ViewBag.Title = "Chat";
}
<h2>Chat</h2>
<div id="divLogin" class="mylogin">

    User Name:<input id="txtUserName" type="text" /><br />
    <br />Password :   <input id="txtPassword" type="password" /><br />
    <input id="btnLogin" type="button" value="Login" />
    <div id="divalarm"></div>
</div>
<div id="divChat" class="mylogin">
    <input type="text" id="txtMessage" />
    <input type="button" id="SendMessage" value="broadcast" />
    <input type="hidden" id="displayname" />
    <ul id="listMessages"></ul>

</div>
@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-2.0.3.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script>
        $(function () {
            $("#divChat").hide();
            $("#divLogin").show();
            var IWannaChat = $.connection.chatHub;
            IWannaChat.client.addMessage = function (name, message) {
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page. 
                $('#listMessages').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };

            $("#SendMessage").click(function () {
                IWannaChat.server.send($('#txtUserName').val(), $('#txtMessage').val());
            });
            $("#btnLogin").click(function () {
                $("#divChat").show();
                $("#divLogin").hide();
            });

            $.connection.hub.start();


        });





        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>

}

ChatHub.cs:

public void send(string username, string message)
        {
            Clients.All.addMessage(username, message);

        }
        public void send1(string message)
        {
            Console.WriteLine("hi");

        }

感谢。

0 个答案:

没有答案