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的更多相关文章

  1. website project team member 角色及开发过程概念图

    一个web项目的团队往往具有以下角色的人员组成: project stakeholder(client or business owner)产品经理 Project manager 项目经理 prod ...

  2. Build website project by roslyn through devenv.com

    1.fetch the source code2.compile controls project3.copy files under bin folder of controls to bin fo ...

  3. SignalR 教程二 服务端广播

    转帖官方教程:Tutorial: Server Broadcast with SignalR 2 http://www.asp.net/signalr/overview/getting-started ...

  4. 一起了解 .Net Foundation 项目 No.19

    .Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. Salesforce To ...

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

    AxeSlide斧子演示:PPT的另一种可能       一款简单有趣的演示文稿制作软件     AxeSlide斧子演示(www.axeslide.com),是一款简单有趣的演示文稿制作软件,基于H ...

  6. [转]在SqlServer 中解析JSON数据

      在Sqlserver中可以直接处理Xml格式的数据,但因为项目需要所以要保存JSON格式的数据到Sqlserver中在博客:Consuming JSON Strings in SQL Server ...

  7. content management system

    Defination of CMS: The definition of a CMS is an application (more likely web-based), that provides ...

  8. sso单点登录解决方案收集

    本文来自:http://blog.csdn.net/huwei2003/article/details/6038017 我的想法是使用集中验证方式,多个站点集中Passport验证. 主站:Passp ...

  9. 免费的Visual Studio的插件

    在做了深入(的)研究之后(通过在google网站搜索),,我编译了15个免费Visual Studio 2005插件表..其中一些插件将提高您(的)代码(的)质量,,另外一些能使您编译(的)更快,,但 ...

随机推荐

  1. C# 词法分析器(七)总结

    系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 在之前的六篇文章中,我比较详细的介绍了与词法分析器相 ...

  2. EF框架step by step(4)—DBcontext应用于已存在数据库

    EF4.1有三种方式来进行数据操作及持久化.分别是Database-First,Model-First,Code-first,前面都已经简单介绍过了.下面简单小结一下:1.Database First ...

  3. 【SAP BO】【WEBI】【转】Webi实现动态选择度量

    我们都知道Web Intelligence具有高级的分析功能,是一个非常灵活的报表工具.在这篇文章里,我会演示一个使用Webi实现动态选择度量对象的方案.首先解释一下什么是”动态选择度量”:例如我们有 ...

  4. 【BZOJ1827】[Usaco2010 Mar]gather 奶牛大集会 树形DP

    [BZOJ][Usaco2010 Mar]gather 奶牛大集会 Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来 ...

  5. css3+js打造炫酷图片展示

    <!DOCTYPE html> <html onselectstart="return false"> <!-- onselectstart=&quo ...

  6. [Cocos2D-x For WP8]Box2D物理引擎

    物理引擎通过为刚性物体赋予真实的物理属性的方式来计算运动.旋转和碰撞反映.为每个游戏使用物理引擎并不是完全必要的—简单的“牛顿”物理(比如加速和减速)也可以在一定程度上通过编程或编写脚本来实现.然而, ...

  7. 野路子土鳖ASP.NETer谈互联网的发展趋势

    有些话别在憋在心里太久了,容易伤身体,总得找个借口吐槽一下,今日不小心看到园有关于互联网发函趋势的文章,作为一名ASP.NET程序员,自然要说说什么是趋势,机会找到了,好高兴. http://zzk. ...

  8. linq中join的用法

    join方法 public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>( this IEnu ...

  9. linux命令之 top, free,ps

    linux终端查看cpu和内存使用情况 t一.op进入全屏实时系统资源使用信息查看 PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT:进程占 ...

  10. [FMS]FMS流媒体服务器onStatus介绍说明

    在FlashCom中的Camera, Microphone, LocalConnection, NetConnection,NetStream和 SharedObject对象都提供了事件响应,onst ...