SignalR in a WebSite Project

Posted jianiu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

 

以上是关于SignalR in a WebSite Project的主要内容,如果未能解决你的问题,请参考以下文章

[Python] Create a minimal website in Python using the Flask Microframework

In an ASP.NET website with a codebehind at what point are the .cs files compiled?

There are no devices registered in your account on the developer website

websites of a professor in UTS

Create a website with iis and NLB

Is EnableViewStateMAC=true compulsory for ViewStateEncryption in an ASP.Net Website?