介尘部落

文学|音乐|休闲娱乐|计算机技术|地球科学|社会学——知识成就命运


SignalR长连接PersistentConnection的使用

Server端:

   public class MyConnection : PersistentConnection
    {
        private static IConnection connection;

        public MyConnection()
        {
            connection = Connection;
        }
        public IConnection GetConnection()
        {
            return Connection;
        }
        protected override Task OnConnected(IRequest request, string connectionId)
        {
            return Connection.Broadcast("Connection " + connectionId + " connected");
        }
        protected override Task OnReconnected(IRequest request, string connectionId)
        {
            return Connection.Broadcast("Client " + connectionId + " re-connected");
        }
        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            var cmd = JsonConvert.DeserializeObject<KeyValuePair<string, string>>(data);
            if (cmd.Key == "group")
            {
                Groups.Add(connectionId, cmd.Value);
            }

            var info = data + ". ConnectionId is [" + connectionId + "] in " + cmd.Value;
            // return Connection.Send(connectionId, info);   

            // Broadcast data to all clients
            return Connection.Broadcast(info);
        }

        protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
        {
            return Connection.Broadcast("Connection " + connectionId + " disconncted");
        }


    }

在PersistentConnection管理类外调用Persistent类方法

 GlobalHost.ConnectionManager.GetConnectionContext<MyConnection>().Groups.Send(trade_no, new { state = 1, data=data });

网页端:

var stateConnect = false;
var connection = null;

//启动连接
function keepconnect() {
    if (connection == null) {
        connection = $.connection('/echo');

        connection.start().done(function () {
           
        });
    }
    connection.received(function (data) {
        if (data.state == 1) {
            //...
        }
        console.log(JSON.stringify(data));
    });
    connection.stateChanged(function (state) {
        if (state.newState == 4) {
            setTimeout(function () {
                console.log('正在重连');
                $.connection.hub.start();
            }, 2000);
        } else if (state.newState == 1) {
            var msg = { "key": "group", "value": 'groupname' };
            connection.send(msg);
            console.log("send:" + JSON.stringify(msg));
            stateConnect = true;
        }
        var stateConversion = { 0: 'connecting', 1: 'connected', 2: 'reconnecting', 4: 'disconnected' };
        console.log('SignalR state changed from: ' + stateConversion[state.oldState]
         + ' to: ' + stateConversion[state.newState]);
    });
}
阅读全文
公众号-介尘阅读时光
赞赏支持

0 Responses to “SignalR长连接PersistentConnection的使用”

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

×