SignalR 客户端 C# Windows 应用程序使用 JavaScript (不使用任何 c# 代码)

SignalR client C# Windows Application using JavaScript (Without using any c# code)

我想使用 java 脚本制作客户端 Windows 应用程序,方法是将我的 Windows 应用程序设为 webBrowser1(与 PhoneGap 的想法相同)。我编写 html 页面,其中包含 SignalR java 带有必要 src 的脚本代码。

这是 Windows 应用程序中的代码:

webBrowser1.Url = new Uri(@"D:\Signal.html");  

这是Signal.html中的src:

<script src="json2.js"></script>
<script src="jquery-1.6.4.js"></script>
<script src="jquery.signalR-2.2.0.js"></script>
<script src="http://localhost:5539/signalr/hubs" type="text/javascript"></script>

不幸的是,它不起作用!!怎么了??

SignalR.html的孔码在这里

<!DOCTYPE html>
<html lang="en">
<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" placeholder="Message" />        
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
    <div style="background-color:GrayText; height:30%; text-align:center; ">
        <h1>Groups</h1>
        <input type="text" id="name" placeholder="User Name" />
        <input type="text" id="groupName" placeholder="Group Name"/>
        <input type="button" id="broadcast" value="Broadcast" />
        <input type="button" id="join" value="Join" />
        <input type="button" id="leave" value="Leave" />
        <input type="button" id="refresh" value="Leave" />



        </div>
    <script src="json2.js"></script>
    <script src="jquery-1.6.4.js"></script>
    <script src="jquery.signalR-2.2.0.js"></script>
    <script src="http://localhost:5539/signalr/hubs" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            //Set the hubs URL for the connection
            $.connection.hub.url = "http://localhost:5539/signalr";

            // Declare a proxy to reference the hub.
            var chat = $.connection.chatingHub;
            var counterh = $.connection.ConnectionCounter;

            //counterh.client.NumberOfConnter = function (count) { $("div").append(count); }

            chat.client.displayText = function (name, message) {
                $('#messages').append('<li>' + name + ' said: ' + message + '</li>');
            };

            chat.client.le = function (id) { alert(id); }
            chat.client.alertJoin = function (namePersonJoined) { $('#messages').append('<li>' + namePersonJoined + " Join to the Group" + '</li>').css("background-color", "white"); }
            chat.client.alertLeave = function (namePersonLeaved) { $('#messages').append('<li>' + namePersonLeaved + " Leave from the Group" + '</li>'); }
            // Create a function that the hub can call to broadcast messages.
            chat.client.addMessage = 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.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });

                $("#broadcast").click(function () {
                    chat.server.broadcastMessage({
                        Name: $('#name').val(),
                        Message: $('#message').val(), Group: $('#groupName').val()
                    });
                });

                $("#join").click(function () {
                    chat.server.join($('#groupName').val(), $('#name').val());
                });

                $("#leave").click(function () {
                    chat.server.leave($('#groupName').val(), $('#name').val());
                });
                $("#refresh").click(function () {
                    chat.server.nowID();
                });
            });
        });
    </script>
</body>
</html>

我认为这是一个 CORS 问题。

您的 signalR 客户端在 D:\SignalR.html

您的 signalR 服务器位于 http://localhost:5539/signalr/hubs

意思是跨源信号R调用。您需要启用 CORS 才能让它工作。