วันนี้จะมาแนะนำเรื่องการพัฒนาระบบด้วย SignalR ซึ่งเป็น Open Source ของ .NET Library สำหรับสร้่าง Web Application ที่ต้องการตอบสนองกับ User แบบ Live หรือมีการ Update ข้อมูลแบบ Real Time
SignalR ช่วยลดความยุ่งยากในการสร้าง Real-Time Application โดยรวม ASP.NET Server Library และ JavaScript Client Library เพื่อให้ง่ายต่อการจัดการ Client-Server Conention
1. สร้าง Project Web Application โดยเลือกเป็น ASP.NET Empty Web Application ดังรูป
2.สร้าง SignalR Hub Class ขึ้นมา ชื่อ ChatHub.cs
3.จากนั้นให้ทำการ Install Microsoft.AspNet.SignalR โดยเข้าที่ Tools | Library Package Manager | Package Manager Consol แล้วพิมพ์ install-package Microsoft.AspNet.SignalR เมื่อติดตั้งเรียบร้อยแล้ว Solution จะมีการ Reference Microsoft.AspNet.SignalR และเพิ่ม Scripts jquery.signalR-1.1.2.js เข้าไปใน Solution ดังรูป
4.จากนั้นเพิ่ม Code ที่ ChatHub.cs ดังนี้
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRChat
{
    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.broadcastMessage(name, message);
        }
    }
}6.ที่ Global.asax.cs ใหั้เพิ่ม Code ดังนี้
- ที่ส่วน using statements
using System.Web.Routing;
using Microsoft.AspNet.SignalR;Application_Start ของ Global Class เพือทำการ Register Default Route สำหรับ SignalR Hub ดังนี้using System.Web.Routing ดังนี้ // Register the default hubs route: ~/signalr/hubs
RouteTable.Routes.MapHubs();<!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.8.2.min.js" ></script>
    <!--Reference the SignalR library. -->
    <script src="/Scripts/jquery.signalR-1.0.0.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.broadcastMessage = 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>:  ' + 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();
                });
            });
        });
    </script>
</body>
</html>การทำงานจะเป็นการสื่อสารกันระหว่าง SignalR jQuery library และ SignalR hub และจำเป็นต้องประกาศ proxy เพื่ออ้างอิงถึง Hub ตาม Code นี้
var chat = $.connection.chatHub; จากนั้นก็ืำืำทำการสร้าง callback function โดย ็Hub Class บน Server จะเรียกฟังก์ชั่นเพื่อส่ง Update Content ไปยังเครื่อง Client แต่ละเครื่อง
chat.client.broadcastMessage = 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>:  ' + encodedMsg + '</li>');
    }; $.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();
        });
    }); 
 
ไม่มีความคิดเห็น:
แสดงความคิดเห็น