asp.net 推模式 用SignalR實現

來源:互聯網
上載者:User

顯示介面

打開3個這樣的介面,再一個中輸入發送資訊,在其他介面中會同時得到數據。

1.下載 SignalR jquery.signalR.js,並且引用。

2.建立MVC工程。

3.在Global.asax中加入註冊項目,註冊連接項目

       protected void Application_Start()
        {
            RouteTable.Routes.MapConnection<myconnection>("echo", "echo/{*operation}");
        }

4.建立myconnection類,繼承PersistentConnection

重寫 ,其中clientId是發送數據的用戶資訊,可以指定給特定用戶或者用戶組   

protected override Task OnReceivedAsync(string clientId, string data)
        {
            // Broadcast data to all clients

    //在這裡進行數據處理
            data = string.Format("數據是:{0} 時間是:{1}", data, DateTime.Now.ToString());
            return Connection.Broadcast(data);
        }

代碼如下:

View Code

   public  class myconnection : PersistentConnection    {        protected override Task OnReceivedAsync(string clientId, string data)        {            // Broadcast data to all clients            data = string.Format("數據是:{0} 時間是:{1}", data, DateTime.Now.ToString());            return Connection.Broadcast(data);        }    }

5.在Index.cshtml頁面中進行調用

View Code

<script type="text/javascript">    $(function () {        var connection = $.connection('echo');        connection.received(function (data) {            $('#messages').append('<li>' + data + '</li>');        });        connection.start();        $("#broadcast").click(function () {            connection.send($('#msg').val());        });    });</script>

介面代碼如下:

View Code

@{    ViewBag.Title = "Home Page";}<script type="text/javascript">    $(function () {        var connection = $.connection('echo');        connection.received(function (data) {            $('#messages').append('<li>' + data + '</li>');        });        connection.start();        $("#broadcast").click(function () {            connection.send($('#msg').val());        });        $("#btnStop").click(function () {            connection.stop();        });    });</script><h2>@ViewBag.Message</h2><input type="text" id="msg" /><input type="button" id="broadcast" value="發送" /><input type="button" id="btnStop" value="停止" /><ul id="messages"></ul>

可以開始,停止。

var connection = $.connection('echo'); 取得連接

囘調函數:

    connection.received(function (data) {
            $('#messages').append('<li>' + data + '</li>');
        });
開始連接 : connection.start();

停止連接: connection.stop();

源碼下載:http://files.cnblogs.com/Teco/MvcApplicationSignalR.rar

上述代碼:我註釋掉 return Connection.Broadcast(data);

只給自己發送,如果要全部發送,請使用 return Connection.Broadcast(data);

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.