大概是只仓鼠 2022-03-11 16:04 采纳率: 78.7%
浏览 38
已结题

signalR这个后端服务器地址是哪个?为什么我用其他方法连不上(语言-c#)

尝试过http://localhost:2531/signalr/chatHub
或是http://localhost:2531
以及http://localhost:2531/chat都不对

img

/*!
 * ASP.NET SignalR JavaScript Library v2.0.0
 * http://signalr.net/
 *
 * Copyright Microsoft Open Technologies, Inc. All rights reserved.
 * Licensed under the Apache 2.0
 * https://github.com/SignalR/SignalR/blob/master/LICENSE.md
 *
 */

/// <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.chatHub = this.createHubProxy('chatHub'); 
        proxies.chatHub.client = { };
        proxies.chatHub.server = {
            changeImage: function (email) {
            /// <summary>Calls the ChangeImage method on the server-side ChatHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
            /// <param name=\"email\" type=\"String\">Server side type is System.String</param>
                return proxies.chatHub.invoke.apply(proxies.chatHub, $.merge(["ChangeImage"], $.makeArray(arguments)));
             },

            changeNickname: function (newName) {
            /// <summary>Calls the ChangeNickname method on the server-side ChatHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
            /// <param name=\"newName\" type=\"String\">Server side type is System.String</param>
                return proxies.chatHub.invoke.apply(proxies.chatHub, $.merge(["ChangeNickname"], $.makeArray(arguments)));
             },

            send: function (message) {
            /// <summary>Calls the Send method on the server-side ChatHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
            /// <param name=\"message\" type=\"String\">Server side type is System.String</param>
                return proxies.chatHub.invoke.apply(proxies.chatHub, $.merge(["Send"], $.makeArray(arguments)));
             }
        };

        return proxies;
    };

    signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false });
    $.extend(signalR, signalR.hub.createHubProxies());

}(window.jQuery, window));

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Simple chat demo with hubs</title>
    <script src="Scripts/jquery-1.6.4.min.js"></script>
    <script src="Scripts/jquery.signalR-2.0.0.min.js"></script>
    <script src="Scripts/chat.js"></script>
    <link href="Styles/Chat.css" rel="stylesheet" />
</head>
<body>
    <h1>Simple chat </h1>
    <p id="powered"><a href="http://www.gravatar.com" target="_blank">Powered by gravatar</a></p>
    <div>
        <div id="user-info" style="display: none">
            <img src="http://www.gravatar.com/avatar.php?gravatar_id=0&size=20" />
            <span id="username"></span>
            <a href="#" id="change-username">(change nick)</a>
            <a href="#" id="change-image">(change image)</a>
        </div>
        <input type="text" id="text" />
        <button id="send">Send</button>
    </div>
    <div id="users-panel">
        <h3>Users</h3>
        <ul id="users"></ul>
    </div>
    <ul id="chatpanel"></ul>

    <script>
        $(function () {
            var chatHub = $.connection.chatHub;
            $.connection.hub.url = "/chat";
            $.connection.hub.logging = true;
            
            chatHub.client.newUserNotification = newUserNotification;
            chatHub.client.nicknameChangedNotification = nicknameChangedNotification;
            chatHub.client.imageChangedNotification = imageChangedNotification;
            chatHub.client.userDisconnectedNotification = userDisconnectedNotification;

            chatHub.client.welcome = welcome;
            chatHub.client.message = addMessageToPanel;

            $.connection.hub.start().done(function () {
                $("#send").click(function () {
                    chatHub.server.send($("#text").val())
                                        .fail(function (err) {
                                            alert(err);
                                        });
                    $("#text").val("").focus();
                });
                $("#change-username").click(function () {
                    var newName = prompt("New username:", $("#username").text());
                    if (newName) {
                        chatHub.server.changeNickname(newName);
                    }
                    return false;
                });
                $("#change-image").click(function () {
                    var newMail = prompt("Your email (registered in gravatar.com):", "");
                    if (newMail) {
                        chatHub.server.changeImage(newMail);
                    }
                    return false;
                });
            });

            function welcome(assignedNickname, userList) {
                var result = "";
                for (var i = 0; i < userList.length; i++) {
                    var user = userList[i];
                    result += getUserListItem(user);
                }
                $("#users").empty();
                $("#users").append(result);
                $("#user-info").show();
                $("#username").text(assignedNickname);
            }

            function userDisconnectedNotification(user) {
                var userElement = getUserElement(user.Id);
                if (userElement.length > 0) {
                    addMessageToPanel(user.Name + " left the chat.", "system");
                    userElement.remove();
                }
            }

            function newUserNotification(user) {
                if (getUserElement(user.Id).length == 0) {
                    $("#users").append($(getUserListItem(user)));
                    addMessageToPanel("New user arrived: " + user.Name + ". Welcome!", "system");
                }
            }
            function nicknameChangedNotification(user, oldName) {
                var userElement = getUserElement(user.Id);
                if (userElement.length > 0) {
                    userElement.replaceWith($(getUserListItem(user)));
                    addMessageToPanel(oldName + " is now " + user.Name + ".", "system");
                }
                if (user.Id === $.connection.hub.id) {
                    $("#username").text(user.Name);
                }
            }
            function imageChangedNotification(user) {
                var userElement = getUserElement(user.Id);
                if (userElement.length > 0) {
                    userElement.replaceWith($(getUserListItem(user)));
                    addMessageToPanel(user.Name + " has a new image.", "system");
                }
                if (user.Id === $.connection.hub.id) {
                    $("#user-info img").prop("src", user.Image);
                }
            }

            function addMessageToPanel(message, type) {
                var $panel = $("#chatpanel");
                $panel.append("<li class='" + type + "'>" + message + "</li>");
                $panel.scrollTop($panel[0].scrollHeight);
            }

            function getUserListItem(user) {
                return "<li id='u" + user.Id + "'><img src='" + user.Image + "' /><div>" + user.Name + "</div></li>";
            }
            function getUserElement(id) {
                return $("#u" + id);
            }
        });
    </script>
</body>
</html>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 3月14日
    • 修改了问题 3月11日
    • 创建了问题 3月11日

    悬赏问题

    • ¥30 关于#微信#的问题:微信实名不绑卡 可以实现吗 有没有专家 可以解决
    • ¥15 (标签-考研|关键词-set)
    • ¥15 求修改代码,图书管理系统
    • ¥15 请问有没求偏多标签数据集yeast,reference,recreation,scene,health数据集。
    • ¥15 传感网应用开发单片机实训
    • ¥15 Delphi 关于sAlphaImageList使用问题
    • ¥15 寻找将CAJ格式文档转txt文本的方案
    • ¥15 shein测试开发会问些啥我是写java的
    • ¥15 关于#单片机#的问题:我有个课程项目设计,我想在STM32F103veTX单片机,M3主控模块上设计一个程序,在Keil uVision5(C语言)上代码该怎么编译?(嫌钱少我可以加钱,急急急)
    • ¥15 opnet仿真网络协议遇到问题