zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Python笔记-面向对象编程
    大学课后答案微信小程序项目实践(1)
    用weexplus从0到1写一个app(2)-页面跳转和文章列表及文章详情的编写
    react基础学习和react服务端渲染框架next.js踩坑
    基于weex的app开发脚手架weexplus学习笔记
    用weexplus从0到1写一个app
    Vue与React两个框架的区别对比
    mpvue学习笔记-之微信小程序数据请求封装
    Flutter/Dart import导入文件关键字总结
    55个提高你CSS开发效率的必备片段(转自:前段日刊)
  • 原文地址:https://www.cnblogs.com/mingjia/p/5522843.html
Copyright © 2011-2022 走看看