ASP.Net, PHP, MVC vs Web Programlama Yazılım ve Sistem Güvenliği

.Net MVC SignalR

Nuget’ten Microsoft.AspNet.SignalR paketleri uygulamaya referans edilir.

//ChatHub.cs

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the addNewMessageToPage method to update clients.
        Clients.All.addNewMessageToPage(name, message);
    }

    public void Test()
    {
        Random rnd = new Random();
        int secim = rnd.Next(0,3);
        string renk = "";
        switch (secim)
        {
            case 0:
                renk = "yellow";
                break;
            case 1:
                renk = "red";
                break;
            case 2:
                renk = "blue";
                break;
        }
        Clients.All.pickColor(renk);
    }
}
//HomeController.cs 
public class HomeController : Controller
{
    public ActionResult Chat()
    {
        return View();
    }
}
//Chat.cshtml

@{
Layout = null;
ViewBag.Title = "Chat";
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Chat</title>
<script src="~/Scripts/jquery-1.6.4.min.js"></script>
</head>
<body>
<div>
    <h2>SignalR Test</h2>
    <div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Gönder" />
        <input type="hidden" id="displayname" />
        <ul id="discussion"></ul>
    </div>
    <div id="clrDiv" style="width:800px; height:20px;">
        <button id="colorInput">Renklendir</button>
    </div>
</div>
<script src="~/Scripts/jquery.signalR-2.4.1.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
    window.onload= function() {
        // Reference the auto-generated proxy for the hub.
        var chat = $.connection.chatHub;
        // Create a function that the hub can call back to display messages.
        chat.client.addNewMessageToPage = function (name, message) {
            // Add the message to the page.
            $('#discussion').append('<li><strong>' + htmlEncode(name)
                + '</strong>: ' + htmlEncode(message) + '</li>');
        };
        //metinyakar.net--------------------------------
        chat.client.pickColor = function (color) {
            $("#clrDiv").css("background-color", color);
        };
        //metinyakar.net--------------------------------

        // 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 () {
                sendButton_OnClick();
            });

            //metinyakar.net--------------------------------
            $("#message").keyup(function (e) {
                if (e.keyCode == 13) {
                    sendButton_OnClick();
                }
            });

            $("#colorInput").click(function (e) {
                console.log(e.keyCode);
                chat.server.test();
            });
            //metinyakar.net--------------------------------
        });

        function sendButton_OnClick() {
            // 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();
        }
    };
    // This optional function html-encodes messages for display in the page.
    function htmlEncode(value) {
        var encodedValue = $('<div />').text(value).html();
        return encodedValue;
    };
</script>
</body>
</html>
//Startup.cs
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir