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. Git常用命令举例

    clone一个git project到本地 git clone https://github.com/huahuiyang/network-certification.git 到这个目录下,可以发现有 ...

  2. java中流转化为Object可序列化

    一.PO实体类 public class buisPO{ /** * 业务数据 */ private Bolb buisData; public Blob getBusiData() { return ...

  3. UVA 11427 (概率DP+期望)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局, ...

  4. 几种提高jQuery性能的代码

    1.jQuery对象缓存 如果同一元素被查找多次时,就应该将该jQuery对象缓存起来,不然每次查找都要遍历整个文档. 使用下边的代码做个简单的测试 <!DOCTYPE html PUBLIC ...

  5. Leetcode Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. git的几种回滚 git revert 和 git reset的区别

    git的几种回滚 git revert 和 git reset的区别:强烈建议:对HEAD不熟的话最好不要用HEAD,直接用commitID吧,我遇到的问题:reset HEAD~1之后,可能是别人提 ...

  7. 【BZOJ】2876: [Noi2012]骑行川藏

    题意 给出\(s_i, k_i, v_i', E\),满足\(\sum_{i=1}^{n} k_i s_i ( v_i - v_i' )^2 \le E, v_i > v_i'\),最小化$ \ ...

  8. JS:操作样式表1:行内样式

    //访问元素样式1, stye属性只对行内样式有用 var box = document.getElementById("box"); // alert(box.style.col ...

  9. CSS_css sprite原理优缺点及使用

    CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢 ...

  10. CAS单点登录配置

    见http://download.csdn.net/detail/u010786672/6942715下载.