从mvc app连接到signalrserver

时间:2018-03-25 15:06:10

标签: c# signalr

我已经建立了windows表单app signalr服务器和名为" MyHub"的集线器。我创建了新的MVC项目网站连接到我的Windows窗体服务器,但我堆叠了如何连接到服务器。你能帮我吗?这是我目前的代码。除了" MyHub"目前有一种方法称为"发送"它向所有人发送消息,如何制作类似:HubProxy.On(这是来自win表格),但这里是mvc。

fbr

修改

在服务器端:

@{
    ViewBag.Title = "Chat";
}
<fieldset>
    <legend style="color:orangered">Welcome To  Satya's signalR MVC Group Chat Club</legend>
</fieldset>
<div class="form-group col-xl-12">
    <label style="color: blue; font-style: oblique;font-size: medium" id="label1">Write Your Message Here!</label><br />
    <textarea class="form-control" rows="4" cols="40" id="message" placeholder="Share what's in your mind..."></textarea>
    <br />
    <input type="button" class="btn btn-primary" id="sendmessage" value="Send" />
    <br />
    <br />
    <label style="color: blue;font-style:oblique;font-size:medium" id="label2">Group Chat Conversations History</label>
    <div class="container chatArea">
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
</div>
@section scripts {
    <script src="~/Scripts/jquery.signalR-2.2.3.min.js"></script>
    <script src="~/signalr/hubs"></script>
    <script>

        $(function () {
            var chat = "MyHub"
            chat.hub.url = 'http://xxxxxxxx:4848/';
            chat.hub.qs = { 'username' : 'John' };
            chat.client.Send = function (name, message) {
                $('#discussion').append('<ul style="list-style-type:square"><li><strong style="color:red;font-style:normal;font-size:medium;text-transform:uppercase">' + htmlEncode(name) + '  ' + '<strong style="color:black;font-style:normal;font-size:medium;text-transform:lowercase">said</strong>'
                    + '</strong>: ' + '<strong style="color:blue;font-style:oblique;font-size:medium">' + htmlEncode(message) + '</strong>' + '</li></ul>');
            };
            $('#displayname').val(prompt('Your Good Name Please:', ''));
            $('#message').focus();
            chat.start().done(function () {
                $('#sendmessage').click(function () {
                    chat.client.Send($('#displayname').val(), $('#message').val());
                    $('#message').val('').focus();
                });
            });
        });
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}

现在在MVC客户端我有:

class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            //app.UseCors(CorsOptions.AllowAll);
            //app.MapSignalR();
             // Branch the pipeline here for requests that start with "/signalr"
            app.Map("/signalr", map =>
            {
                // Setup the CORS middleware to run before SignalR.
                // By default this will allow all origins. You can 
                // configure the set of origins and/or http verbs by
                // providing a cors options with a different policy.
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    // EnableJSONP = true
                };
                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch already runs under the "/signalr"
                // path.

                hubConfiguration.EnableDetailedErrors = true;
                map.RunSignalR(hubConfiguration);
            });
        }
    }


    public class MyHub : Hub
    {
     public async Task Send(string name, string message)
        {
            await Clients.All.addMessage(name, message);
            uListHistory.Add(new ChatHistory { name = name, message = message });
        }
....

1 个答案:

答案 0 :(得分:0)

服务器和客户端信号器版本必须匹配

根据您的错误消息,您似乎没有正确的信号器版本。

提示:下次请尽量自己做调试。为此,在您的代码中添加跟踪消息,并在浏览器窗口中显示错误。

相关问题