信号器演示中的问题 [英] problems in signalr demo

查看:122
本文介绍了信号器演示中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的集线器课程

my hub class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalR_mvc
{
    public class ChatHub : Hub
    {        
        public void Send(string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(message);
        }        
    }
}



index.cshtml


index.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
    <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 src="Scripts/jquery-1.6.4.min.js" ></script>    
    <script src="Scripts/jquery.signalR-1.0.0-rc1.js"></script>    
    <script src="/signalr/hubs"></script>    
    <script type="text/javascript">
        $(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) {
                $('#discussion').append('<li>' + message + '</li>');
            };
            // Start the connection.
            $.connection.hub.start();
            $('#sendmessage').click(function () {
                // Html encode display name and message.                 
                var encodedMsg = $('<div />').text($('#message').val()).html();
                // Call the Send method on the hub. 
                chat.server.send(encodedMsg);
                // Clear text box and reset focus for next comment.                    
            });           
        });
    </script>



MessagesPage.cshtml


MessagesPage.cshtml

@{
    ViewBag.Title = "Index";
}
    
<h2>Index</h2>
    <div class="container">       
        <ul id="discussion">
        </ul>
    </div>        
    <script src="Scripts/jquery-1.6.4.min.js" ></script>    
    <script src="Scripts/jquery.signalR-1.0.0-rc1.js"></script>    
    <script src="/signalr/hubs"></script>   
     
    <script type="text/javascript">
        $(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) {
                $('#discussion').append('<li>' + message + '</li>');
            };
            // Start the connection.
            $.connection.hub.start();          
        });
    </script>





在index.cshtml消息中添加消息但在MessagesPage.cshtml页面中消息没有添加ie。 chat.client.broadcastMessage没有在messagesPage.cshtml页面中调用,那么问题是什么。

我也想知道Signalr的优点和缺点。

谢谢。



In index.cshtml messages is adding but in MessagesPage.cshtml page the messages is not adding ie. chat.client.broadcastMessage is not calling in messagesPage.cshtml page,So what is the problem.
and also i want know advantages and disadvantages of Signalr.
Thank you.

推荐答案

(function(){
// 声明代理以引用集线器。
var chat =
(function () { // Declare a proxy to reference the hub. var chat =


.connection.chatHub ;
// 创建集线器可以调用广播消息的功能。
chat.client.broadcastMessage = function(message){
.connection.chatHub; // Create a function that the hub can call to broadcast messages. chat.client.broadcastMessage = function (message) {


' #讨论')。append(' < li>' + message + ' < / li>');
};
// 开始连接。
('#discussion').append('<li>' + message + '</li>'); }; // Start the connection.


这篇关于信号器演示中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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