• 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>
  • 相关阅读:
    USACO 1.2 Broken Necklace
    USACO 1.2 Friday the Thirteenth
    USACO 1.1 Greedy Gift Givers
    USACO 1.1 Your Ride Is Here
    CSP考试策略
    CF444A DZY Loves Physics【结论】
    树状数组-复习笔记
    CF792E Colored Balls【思维】
    USACO4.4 Shuttle Puzzle【bfs+优化】
    拓扑排序-学习笔记
  • 原文地址:https://www.cnblogs.com/mingjia/p/5522843.html
Copyright © 2020-2023  润新知