Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC4 ASP.NET4.0
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.
Posted 30-Dec-12 23:56pm
hit91452

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150301.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2015
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