架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 11732|回复: 1

[ASP.NET] mvc4.0 如何使用SignalR(二)

[复制链接]
发表于 2017-7-5 11:15:08 | 显示全部楼层 |阅读模式
首先,我在项目下面新建了一个Hubs文件夹

并且,在文件夹下面新建了一个PositionVehicle的文件,如下图:

QQ截图20170705110308.jpg

重新生成项目,然后,我们在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));


然后,我们来测试一下,代码如下:



测试结果如下:

QQ截图20170705105555.jpg
其中收到的前2条消息,都是,我们自己发的,自己也都接收到了,第3条消息,是我开了另外一个浏览器发的,我们同样也接收到了。

收到消息,并不是用的websocket,其实是http轮询的,因为,我们js都报错了

至于,为什么报js错误,我们下次再说,

QQ截图20170705105607.jpg







上一篇:asp.net mvc4.0 安装SignalR(一)
下一篇:Error Code: 1175. You are using safe update mode and you tried to update a ta...
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2017-7-5 14:53:15 | 显示全部楼层
.NET使用SignalR出现 Error during WebSocket handshake
在开发的过程中,碰到如下错误:WebSocket connection to 'ws://*****' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET。

查找websocket使用不了的原因,是由于web.config文件没有配置好。SignalR正确运行需要在system.web节点下添加下面的配置项:



targetFramework设置成4.5
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-20 17:51

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表