@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<script src="~/signalr/hubs" type="text/javascript"></script>
@*<script src="/signalr/hubs" type="text/javascript"></script>*@
<div>
<script type="text/javascript">
var chat;
$(function () {
// Created proxy
chat = $.connection.myChat;
// Assign a function to be called by the server
chat.client.addMessage = onAddMessage;
// Register a function with the button click
$("#broadcast").click(onBroadcast);
// Start the connection
$.connection.hub.start().done(function (a) {
console.log("成功");
console.log(a);
});
});
function onAddMessage(message) {
// Add the message to the list
$('#messages').append('<li>' + message + '</li>');
};
function onBroadcast() {
// Call the chat method on the server
chat.server.send($('#message').val());
//.done(function () {
// console.log('Sent message success!');
//})
// .fail(function (e) {
// console.warn(e);
// });;
}
</script>
<input type="text" id="message" />
<input type="button" id="broadcast" value="send" />
<ul id="messages">
</ul>
</div>
[HubName("myChat")]
public class MyChat : Hub
{
/// <summary>
/// Sends the specified message.
/// </summary>
/// <param name="message">The message.</param>
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.All.addMessage(message);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(SignalR.Core.Startup))]
namespace SignalR.Core
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
浙公网安备 33010602011771号