web api集成SignalR无法正常工作

时间:2014-05-25 18:01:52

标签: asp.net-web-api signalr signalr-hub

我错过了什么吗?我不能让SignalR使用WebAPI。我不知道我是否有一些与绘图有关的特殊内容。当我创建一个空项目时,我得到了这段代码。但是,当我尝试合并SignalR时,这不起作用。

OWIN

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(WebApplication3.Startup))]

namespace WebApplication3
{
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
        // For more information on how to configure your application, visit     http://go.microsoft.com/fwlink/?LinkID=316888
    }
}
}

ChatHub

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;

namespace WebApplication3
{
public class ChatHub : Hub
{
    public void Hello()
    {
        Clients.All.hello();
    }
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        var n = Context.User.Identity.Name;

        Clients.All.broadcastMes(n, message);
        //   Clients.Client.
    }

    public override Task OnConnected()
    {
        return base.OnConnected();
    }
    public override Task OnDisconnected()
    {
        // Add your own code here.
        // For example: in a chat application, mark the user as offline, 
        // delete the association between the current connection id and user name.
        return base.OnDisconnected();
    }
}
}

客户

<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
<style type="text/css">
    .container {
        background-color: #99CCFF;
        border: thick solid #808080;
        padding: 20px;
        margin: 20px;
    }
 </style>
</head>
<body>
<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="discussion"></ul>
</div>
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.min.js"></script>
<!--Reference the SignalR library. -->
<script src="Scripts/jquery.signalR-2.0.3.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="signalr/hubs"></script>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMes = function (name, message) {
            // Html encode display name and message.
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page.
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };

        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {




            $('#sendmessage').click(function () {
                // Call the Send method on the hub.

                chat.server.hello();
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment.
                $('#message').val('').focus();
            });
        });
    });
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

客户端自动断开连接。我通过重新连接断开连接来修复它。