|
首先,我在项目下面新建了一个Hubs文件夹
并且,在文件夹下面新建了一个PositionVehicle的文件,如下图:
重新生成项目,然后,我们在view视图页面引用下面两个js文件
注意!!!
“/Scripts/jquery.signalR-2.2.2.min.js” 文件是安装SignalR会增加的,在物理磁盘存在的
“/signalr/hubs”是SignalR自动生成的,在物理磁盘找不到的!
我们来看一下SignalR生成的js文件,如下:
/*!
* ASP.NET SignalR JavaScript Library v2.2.2
* http://signalr.net/
*
* Copyright (c) .NET Foundation. All rights reserved.
* Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
*
*/
/// <reference path="..\..\SignalR.Client.JS\Scripts\jquery-1.6.4.js" />
/// <reference path="jquery.signalR.js" />
(function ($, window, undefined) {
/// <param name="$" type="jQuery" />
"use strict";
if (typeof ($.signalR) !== "function") {
throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
}
var signalR = $.signalR;
function makeProxyCallback(hub, callback) {
return function () {
// Call the client hub method
callback.apply(hub, $.makeArray(arguments));
};
}
function registerHubProxies(instance, shouldSubscribe) {
var key, hub, memberKey, memberValue, subscriptionMethod;
for (key in instance) {
if (instance.hasOwnProperty(key)) {
hub = instance[key];
if (!(hub.hubName)) {
// Not a client hub
continue;
}
if (shouldSubscribe) {
// We want to subscribe to the hub events
subscriptionMethod = hub.on;
} else {
// We want to unsubscribe from the hub events
subscriptionMethod = hub.off;
}
// Loop through all members on the hub and find client hub functions to subscribe/unsubscribe
for (memberKey in hub.client) {
if (hub.client.hasOwnProperty(memberKey)) {
memberValue = hub.client[memberKey];
if (!$.isFunction(memberValue)) {
// Not a client hub function
continue;
}
subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue));
}
}
}
}
}
$.hubConnection.prototype.createHubProxies = function () {
var proxies = {};
this.starting(function () {
// Register the hub proxies as subscribed
// (instance, shouldSubscribe)
registerHubProxies(proxies, true);
this._registerSubscribedHubs();
}).disconnected(function () {
// Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs.
// (instance, shouldSubscribe)
registerHubProxies(proxies, false);
});
proxies['positionVehicle'] = this.createHubProxy('positionVehicle');
proxies['positionVehicle'].client = { };
proxies['positionVehicle'].server = {
hello: function (name) {
return proxies['positionVehicle'].invoke.apply(proxies['positionVehicle'], $.merge(["Hello"], $.makeArray(arguments)));
}
};
return proxies;
};
signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
$.extend(signalR, signalR.hub.createHubProxies());
}(window.jQuery, window));
然后,我们来测试一下,代码如下:
测试结果如下:
其中收到的前2条消息,都是,我们自己发的,自己也都接收到了,第3条消息,是我开了另外一个浏览器发的,我们同样也接收到了。
收到消息,并不是用的websocket,其实是http轮询的,因为,我们js都报错了
至于,为什么报js错误,我们下次再说,
|
上一篇:asp.net mvc4.0 安装SignalR(一)下一篇:Error Code: 1175. You are using safe update mode and you tried to update a ta...
|