尝试过http://localhost:2531/signalr/chatHub
或是http://localhost:2531
以及http://localhost:2531/chat都不对
/*!
* 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. 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. 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. 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>