This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19123|Reply: 1

[ASP.NET] mvc 4.0 How to use SignalR (2)

[Copy link]
Posted on 7/5/2017 11:15:08 AM | | | |
First, I created a new Hubs folder under the project

Also, create a new PositionVehicle file under the folder, as shown in the following figure:



Rebuild the project, and then we reference the following two js files on the view page



Note!!!

The "/Scripts/jquery.signalR-2.2.2.min.js" file is added to the installation of SignalR and exists on the physical disk

"/signalr/hubs" isSignalR is automatically generated, which cannot be found on the physical disk!

Let's take a look at the js file generated by SignalR, as follows:

/*!
* 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));


Then, let's test it out and the code is as follows:



The test results are as follows:


The first two messages we received were both sent by ourselves, and we also received them, and the third message was sent by me who opened another browser, and we also received it.

When I received the message, it was not a websocket, but actually an HTTP poll, because our js reported an error

As for why the js error was reported, we will talk about it next time.









Previous:asp.net mvc4.0 Installing SignalR (1)
Next:Error Code: 1175. You are using safe update mode and you tried to update a ta...
 Landlord| Posted on 7/5/2017 2:53:15 PM |
. .NET appears using SignalRError during WebSocket handshake
During the development process, I encountered the following error: WebSocket connection to 'ws://*****' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET.

The reason why the websocket cannot be used is because the web.config file is not configured. SignalR to run correctly requires adding the following configuration item under the system.web node:



targetFramework set to 4.5
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com