Click here to Skip to main content
12,882,397 members (25,817 online)

problems in signalr demo

hit91 asked:

Open original thread
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
@{
    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
@{
    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>


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.
Tags: MVC4, ASP.NET4

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.170422.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100