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>
时间: 2024-07-31 02:28:32

SignalR in a WebSite Project的相关文章

website project team member 角色

一个web项目的团队往往具有以下角色的人员组成: project stakeholder(client or business owner)产品经理 Project manager 项目经理 producer 制片人 editor/copywriter编辑和文案人员 information architect: 信息架构师 Graphic designer 图形图像设计师 HTML Developer HTML开发人员(涵盖CSS, javascript开发) Developer 开发人员(往往

Homework1 of Software Project Management-- a project description

There was once a project about establishing a website providing some basic functions such as showing data from a database, searching for some certain data and so on. Initially, I was considering to spend three weeks on this website project, two weeks

SignalR 教程二 服务端广播

转帖官方教程:Tutorial: Server Broadcast with SignalR 2 http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr This tutorial shows how to create a web application that uses ASP.NET SignalR 2 to provide server broadcast fu

[转]在SqlServer 中解析JSON数据

在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server中该作者通过自定义类型的方法实现了对JSON的处理,而且Sqlserver可以查询处理后的数据因此可以在项目中放心的使用 来个例子 Select * from parseJSON('{ "联系人": { "姓名": "huang", "网名&q

NET Core WordPress

NET Core 上运行的 WordPress 在.NET Core 上运行的 WordPress,无需安装PHP既可跨平台运行WordPress. 在Peachpie中实现PHP所需的功能数月后,现在终于可以运行一个真实的应用程序:WordPress. 本文是基于Peachpie https://github.com/iolevel/peachpie Peachpie是一个基于Microsoft的Roslyn的现代PHP编译器. 在.NET上运行WordPress 流行的Phalanger项目

16个不错的Visual Studio插件

Microsoft Visual Studio(简称VS)是美国微软公司的开发工具套件系列产品.VS是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境等等.所写的目标代码适用于微软支持的所有平台,包括Microsoft Windows.Windows Mobile.Windows CE..NET Framework..NET Compact Framework和Microsoft Silverlight.(摘自维基百科) Visua

nw.js 软件推荐:AxeSlide斧子演示:PPT的另一种可能(转)

AxeSlide斧子演示:PPT的另一种可能 一款简单有趣的演示文稿制作软件 AxeSlide斧子演示(www.axeslide.com),是一款简单有趣的演示文稿制作软件,基于Html5 2D/3D技术开发,具有天生的跨平台优势,支持主流的Windows和OSX系统. AxeSlide斧子演示采用全新的演示方式,完全颠覆PPT式的线性演示思维.所有内容都在一张大画布上,内容组织方式类似思维导图.利用平移.旋转和缩放(Zoom),达到镜头推进和拉出的演示效果.同时也支持导入各种你想展示出来的图片

ASP.NET ASHX Handler

Some ASP.NET files are dynamic. They are generated with C# code or disk resources. These files do not require web forms. Instead, an ASHX generic handler is ideal. It can return an image from a query string, write XML, or any other data. Tutorial Fir

基于Ubuntu Server 16.04 LTS版本安装和部署Django之(五):测试项目

1.在windows端使用工具(可以是pythonIDE,也可以是PyCharm等工具)创建项目 django-admin startproject website 2.配置wsgi.py文件: PROJECT_DIR = dirname(dirname(abspath(__file__))) sys.path.insert(0,PROJECT_DIR) from django.core.wsgi import get_wsgi_application os.environ.setdefault