SignalR in a WebSite Project

Question(http://stackoverflow.com/questions/19924678/signalr-in-a-website-project)

 I have a test web application using SignalR that works great.I need to use SignalR in a Website project-It cannot be converted to a web application. I copied my test code into a test wetsite project and I cannot get it to work! I get the "Cannot read property 'client' of undefined" error. Is there anyting that i need to do to get it working in a Websit Project?

Answer:

Put  the Hub Class codefile in App_Code folder

Demo:

First: Map the SignalR 

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

/// <summary>
/// Global 的摘要说明
/// </summary>
namespace ApplicationName
{
    public partial class MyApplication : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });
        }
    }
}

Second: Create SignalR Hub class in App_Code folder

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
public class MyChat : Hub
{
    /// <summary>
    /// Sends the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    public void Send(string message)
    {
        // Call the addMessage method on all clients         
        Clients.All.addMessage(message);
    }
}

Third: In Client Code,you define methods that can be called from the server,and you call methods that run on the server

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script src="/Scripts/jquery-1.6.4.js"></script>
    <script src="/Scripts/jquery.signalR-1.2.2.js"></script>
    <script src="/signalr/hubs"></script>
    <title>SignalR Demo</title>
</head>
<body>
    <script>
        var chat;
        $(function () {
            // Created proxy,此处要特别注意,Hub类的首字母是大写MyChat,但前端使用时,首字母要小写   
            chat = $.connection.myChat;
            // Assign a function to be called by the server        
            chat.client.addMessage = onAddMessage;  
            // Register a function with the button click               
            $("#broadcast").click(onBroadcast);
            // Start the connection        
            $.connection.hub.start().done(function (a) {
                console.log("成功");
                console.log(a);
            });
        });
        function onAddMessage(message) {
            // Add the message to the list    
            $('#messages').append('<li>' + message + '</li>');
        };
        function onBroadcast() {        
            chat.server.send($('#message').val());
        }
    </script>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="message" />
        <input type="button" id="broadcast" value="broadcast" />
        <ul id="messages"></ul>
    </div>
    </form>
</body>
</html>

 

posted on 2016-05-24 11:36  jianiu  阅读(218)  评论(0编辑  收藏  举报